Hi i have one view called Customer.In that view i have one field called Area. I kept one add button near to area. suppose if area is not in the list means i click that add button it open AreaPartialView as popup window.
Now i have one issue in that. That is the data is not passing from partial view to post action method of controller.
My Model
public Nullable AreaID { get; set; }
public string Area { get; set; }
public Nullable CityID{ get; set; }
public string City { get; set; }
My Controller
public ActionResult AreaPartialView()
{
ViewBag.CityID = new SelectList(db.Cities, "CityID", "DisplayName");
return View("AreaPartialView");
}
[HttpPost]
public ActionResult AddAreaInfo(CustomerViewModel objareaVM)
{
var objAreaID = Guid.NewGuid();
ViewBag.CityID = new SelectList(db.Cities, "CityID", "DisplayName", objareaVM.CityID);
@Html.Label("Area" , new { @class = "control-label" })
@Html.TextBoxFor(model => model.Area, new { @class = "form-control", type = "text" ,id ="AreaName"})
@Html.ValidationMessageFor(model => model.Area)
@Html.Label("City")
@Html.DropDownList("CityID", null, "Select", new { @class = "form-control " })
function SaveArea() {
var Area = $("#AreaName").val();
var CityID = $("#CityID").val();
alert(Area);
var AreaAdd = {
"AreaName": Area, "CityID": CityID
};
alert(AreaAdd.AreaName);
alert(AreaAdd.CityID);
$.post("/Customer/AddAreaInfo", AreaAdd,
function (data) { if (data == 0) { location = location.href; } }, 'json').success(function (data) {
alert("success");
//window.close();
});
}
if i click the add button Area partial view will open as popup window and if i enter the details and click Create button it get the values and showing all alert messages. But the problem is it does not pass the Area value to post action method it pass the City ID value but it doesn't pass the Area value to post method. The Area value will be always null.
Please any one cross check my code and tell me the solution .
Advance Thanks..
8 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Sneha KRank #2,496 · 563 reputationPosted Mar 21, 2016, 12:00 AM EDT
0
Shuvojit i gave my id as AreaName and it getting the value var AreaAdd = {
"AreaName": Area, "CityID": CityID
};
alert(AreaAdd.AreaName);
alert(AreaAdd.CityID); it shows the both alert message with the values which i enter but it pass the values to post action method the area value will be null but not cityID
0
Shuvojit HalderRank #2,827 · 503 reputationPosted Mar 21, 2016, 12:26 AM EDT
0
Check your model there is Area field but you are using Areaname in your jquery model. That is why it is null. use
Sneha KRank #2,496 · 563 reputationPosted Mar 21, 2016, 12:47 AM EDT
0
Shuvojit Halder it getting the value in var AreaAdd but when i pass these two values to controller the Area value will be null but not city . this is the issue.
0
Shuvojit HalderRank #2,827 · 503 reputationPosted Mar 21, 2016, 12:51 AM EDT
0
Did you match the same name of jquery model field and your server model field which you have passed the controller?
Shuvojit HalderRank #2,827 · 503 reputationPosted Mar 20, 2016, 11:54 PM EDT
Sneha KRank #2,496 · 563 reputationPosted Mar 21, 2016, 12:00 AM EDT
Shuvojit HalderRank #2,827 · 503 reputationPosted Mar 21, 2016, 12:26 AM EDT
Sneha KRank #2,496 · 563 reputationPosted Mar 21, 2016, 12:34 AM EDT
Sneha KRank #2,496 · 563 reputationPosted Mar 21, 2016, 12:34 AM EDT
Sneha KRank #2,496 · 563 reputationPosted Mar 21, 2016, 12:47 AM EDT
Shuvojit HalderRank #2,827 · 503 reputationPosted Mar 21, 2016, 12:51 AM EDT
Sneha KRank #2,496 · 563 reputationPosted Mar 21, 2016, 1:08 AM EDT