i am new to Ap.net core MVC I am in middle of a project.. i am trying to create a url using jquery and pass that value to controller on form submit.. i can see the value being displayed after i execute a function on button click. I am using loading a partial view on dropdown using jquery im able to populate it and also assign the value to the textbox. But when i try to access the same in controller im not able to find it below is my complete code. I am able to access all the other values but the textbox value which setting through jquery
Below is my View
<form method="post" asp-action="Upsert" enctype="multipart/form-data">
<div class="row px-2 mx-2 backgroundWhite border">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
@if (Model.NavId != 0)
{
<input type="hidden" asp-for="NavId" />
title = "Edit Navigation";
}
<div class="col-12">
<h2 class="text-primary">@title</h2>
<hr class="my-4">
</div>
<div class="col-8">
<div class="form-group row">
<div class="col-5">
<label asp-for="Title"></label>
</div>
<div class="col-7">
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-5">
<label asp-for="Position"></label>
</div>
<div class="col-7">
<input asp-for="Position" class="form-control" />
<span asp-validation-for="Position" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-5">
<label asp-for="NavigateUrl">NavigationUrl</label>
</div>
<div class="col-7">
<input asp-for="NavigateUrl" disabled id="NavigateUrl" name="NavigateUrl" class="form-control" style="color:#000 !important"/>
<span asp-validation-for="NavigateUrl" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-5">
<label>Select Page</label>
</div>
<div class="col-7">
<select id="SelectPage" class="form-control">
<option value="-">Select Page</option>
<option value="Products">Products</option>
<option value="ProductDetails">Product Details</option>
</select>
</div>
</div>
</div>
<div class="col-12 selectDiv mt-2 mb-2">
</div>
<div class="form-group row col-8">
<div class="col-12 offset-4">
@if (Model.NavId != 0)
{
//Edit button and back to list
<partial name="_EditAndBackToListButton" model="Model.NavId" />
}
else
{
//Create button and back to list
<partial name="_CreateAndBackToListButton" />
}
</div>
</div>
</div>
@section Scripts{
<script>
$(document).ready(function () {
$('#SelectPage').change(function () {
var selectedvalue = $(this).children("option:selected").val();
$(".selectDiv").load("/Admin/Nav/GetCategoryOrProducts",
{ option: selectedvalue });
});
});
</script>
<script>
var assignvalue = function (primaryId) {
event.preventDefault();
var selectedvalue = $('#SelectPage').children("option:selected").val();
var navtext = $('#NavigateUrl');
var navurl = "/" + selectedvalue + "/" + primaryId;
navtext.val(navurl);
};
</script>
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
My Controller method
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Upsert(Nav nav)
{
if (ModelState.IsValid)
{
if (nav.NavId == 0)
{
string navurl = HttpContext.Request.Form["NavigateUrl"].ToString(); // I am not able to get the value here it shows null
nav.NavigateUrl = navurl;
_unitOfWork.Nav.Add(nav);
}
else
{
_unitOfWork.Nav.Update(nav);
}
return RedirectToAction(nameof(Index));
}
return View();
}
_CreateAndBackToListButton.cshtmland how do you callassignvaluefunction?2.Also,share the action about how to display your main view here.