I am new to Jquery and creating partial views. I have a "Create Project" button where I am trying to make it so that if the user clicks it the page renders a partial view that contains a form post inside a where the user can input data and can execute OnPost() in the code behind the razor page.
Here is my PartialView: _CreateProject.cshtml
@page
@model Visportfolio.Pages.CreatePortfolioModel
@*
For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@
<form enctype="multipart/form-data" method="post" id="partial">
<label asp-for="Category">Category: </label>
<select asp-for="Category" asp-items="Model.categorylist">
<option value="">---Select Category---</option>
</select>
<label asp-for="SubCategoryId">Subcategory:</label>
<select asp-for="SubCategoryId"><option value="">---Select Subcategory---</option></select>
<div class="row">
<div class="col-md-4">
<label asp-for="ProjectName"></label>
<input asp-for="ProjectName" class="form-control" />
<span class="alert-danger" asp-validation-for="ProjectName"></span>
</div>
</div>
<div class="row">
<div class="col-md-4">
<label asp-for="ProjectDescription"></label>
<input asp-for="ProjectDescription" class="form-control" />
</div>
</div>
<div class="for-group-row">
<label asp-for="FileUpload" class="col-sm-2 col-form-label"></label>
<div class="col-md-4">
<div class="custom-file">
<input asp-for="FileUpload" class="form-control custom-file-input" />
<label class="custom-file-label">Choose File</label>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Create</button>
</div>
</div>
</form>
This is the Button that I monitor onclick event with jquery:
<button class="btn btn-default" id="call_partial">New Project</button>
Then On-Click I want the partial view to be rendered within this div:
<div id="partial">
//the form post from partial here
</div>
Jquery that checks for on-click
$('#call_partial').click(function () {
$('#partial').load("~/Pages/Portfolio/CreatePortfolio/OnGetPartialProject");
});
Note: I have also tried: @Html.Raw(Url.Action("OnGetPartialProject")) instead of what is in the parameter for load() above.
Finally, this is the method where I try to render the partial view
public IActionResult OnGetPartialProject()
{
return Partial("~/Pages/Portfolio/_CreateProject.cshtml");
}
I have tried to follow examples that use MVC and tried to manipulate it with Razor but I am unsure what to fix. I know that the Jquery isn't calling the handler method that is supposed to render the partial view because the breakpoint that I have placed there is never reached. Therefore it leaves me to believe that the Jquery is incorrect but am unsure why. If after fixing that would the rest of the syntax for calling a partial view that I have made here work for my scenario of loading that partial view into the with id="partial" ?