I'm working on ASP.Net MVC.
I have a ViewPage SaveStallMenu.cshtml, which has 2 DropDown Lists
(MF Dropdown, Stalls Dropdown) and a FilerMenu Button
When the page is loaded for the first time, it holds MenuItem of Stall 1.
Now, My question is, when I select an option from MF Dropdownlist and Stall DropdownList, and click Filter Menu button, the contents in the <div id="partialViewDiv"></div> should be refreshed with the Menu Item related to the Stall Selection.
I'm using AJAX for this purpose.
When I click the FilterMenu button
@using (Ajax.BeginForm("uppendStallData", "Admin", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, LoadingElementId = "", UpdateTargetId = "partialViewDiv" }))
{
}
This Should submit my form to uppendStallData ActionMethod, but I don't know why It's not working.
Pardon me, if you find flaws in my explanation of Question. And please try to go through My code and provide your guidance.
I tried many solutions as you can see the Scripts below,but didn't get what I'm trying to do.
Trial 1:
If I use above Ajax.BeginForm, then uppendStallDatadoesn't get ViewModel data POSTed.
[HttpPost]
public PartialViewResult uppendStallData(AdminVM VM)
{
List<StallsModel> data = _provider.GetStallsList();
VM.StallNameDDl = data.ToDictionary(x => x.StallId, x => x.StallName);
List<MFDetailsModel> data1 = _provider.GetMFDetailsList();
VM.MFNameDDl = data1.ToDictionary(x => x.Id, x => x.MFName);
long MFID1 = Convert.ToInt64(VM.StallMenuModel.MFId);
long STallId = Convert.ToInt64(VM.StallMenuModel.StallId);
VM.CompleteMenuByStallIDList = _provider.GetCompleteMenuByStallId_List(STallId);
return PartialView("_MenuTabs", VM);
}
Trial 2:
If I call a as shown below, on FilterMenu button click,
this script sends the dropdownlist selected values to uppendStallData, but after successful execution of the Method in Contoller, fails to return the PartialView and append to <div id="partialViewDiv">.
With Trial2, my script and contoller method is as follow,
<script>
$('#Filter').click(function () {
var MF_Id = $('#StallMenuModel_MFId').val();
var Stall_Id = $('#StallMenuModel_StallId').val();
$.ajax({
url: '@Url.Action("uppendStallData", "Admin")',
data: { MFID: $("#StallMenuModel_MFId").val(), STID: $("#StallMenuModel_StallId").val() },
type: 'POST',
success: function (data) {
$("#partialViewDiv").html(data);
}
});
});
</script>
Controller
public PartialViewResult uppendStallData(string MFID,string STID)
{
List<StallsModel> data = _provider.GetStallsList();
VM.StallNameDDl = data.ToDictionary(x => x.StallId, x => x.StallName);
List<MFDetailsModel> data1 = _provider.GetMFDetailsList();
VM.MFNameDDl = data1.ToDictionary(x => x.Id, x => x.MFName);
long MFID1 = Convert.ToInt64(MFID);
long STallId = Convert.ToInt64(STID);
VM.CompleteMenuByStallIDList = _provider.GetCompleteMenuByStallId_List(STallId);
return PartialView("_MenuTabs", VM);
}
This is Controller code:
[HttpGet]
public ActionResult SaveStallMenu()
{
AdminVM VM = new AdminVM();
List<StallsModel> data = _provider.GetStallsList();
VM.StallNameDDl = data.ToDictionary(x => x.StallId, x => x.StallName);
List<MFDetailsModel> data1 = _provider.GetMFDetailsList();
VM.MFNameDDl = data1.ToDictionary(x => x.Id, x => x.MFName);
VM.StallMenuModel = new StallMenuModel();
VM.CompleteMenuByStallIDList = _provider.GetCompleteMenuByStallId_List(1);
return View(VM);
}
[HttpPost]
public PartialViewResult uppendStallData(string MFID,string STID)
{
List<StallsModel> data = _provider.GetStallsList();
VM.StallNameDDl = data.ToDictionary(x => x.StallId, x => x.StallName);
List<MFDetailsModel> data1 = _provider.GetMFDetailsList();
VM.MFNameDDl = data1.ToDictionary(x => x.Id, x => x.MFName);
long MFID1 = Convert.ToInt64(MFID);
long STallId = Convert.ToInt64(STID);
VM.CompleteMenuByStallIDList = _provider.GetCompleteMenuByStallId_List(STallId);
return PartialView("_MenuTabs", VM);
}