Hi I want to pass the data from partial view to controller
My Partial view Code
@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("sucess");
//window.close();
});
}
My Model(CustomerViewModel)
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);
var ObjArea = new Area()
{
AreaID =objAreaID,
DisplayName = objareaVM.Area,
PrintName = objareaVM.Area,
CityID = objareaVM.CityID,
IsActive = true,
IsDeleted = false,
CreatedDate = DateTime.Now,
EditedDate = DateTime.Now,
LastActiveOn = DateTime.Now,
RowID = Guid.NewGuid(),
CreatedSessionID = Guid.NewGuid(),
EditedSessionID = Guid.NewGuid(),
OfflineMode = false,
OfflineID = Guid.NewGuid()
};
db.Areas.Add(ObjArea);
db.SaveChanges();
ModelState.Clear();
return Json("Customer", JsonRequestBehavior.AllowGet);
}
When i enter the details it get all the values in j query function it getting the value in
var Area = $("#AreaName").val();
var CityID = $("#CityID").val();
and also in
var AreaAdd = { "AreaName": '' + Area + '', "CityID": CityID };
and showing all alert message with values there is no problem in getting the values in j query function . The problem is in passing these values to controller the Area Value will be null but nit CityID. This is my issue . I am facing this issue from long time . please any one help me to resolve this issue..
Advance thanks..
20 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Saillesh PawarPosted Mar 22, 2016, 2:57 AM
Sneha KPosted Mar 22, 2016, 5:52 AM
Saillesh PawarPosted Mar 22, 2016, 5:49 AM
Sneha KPosted Mar 22, 2016, 4:27 AM
Sneha KPosted Mar 22, 2016, 4:10 AM
Saillesh PawarPosted Mar 22, 2016, 4:08 AM
Sneha KPosted Mar 22, 2016, 4:02 AM
Saillesh PawarPosted Mar 22, 2016, 3:54 AM
Sneha KPosted Mar 22, 2016, 3:49 AM
Sneha KPosted Mar 22, 2016, 3:39 AM
Saillesh PawarPosted Mar 22, 2016, 3:36 AM
Sneha KPosted Mar 22, 2016, 3:22 AM
Sneha KPosted Mar 22, 2016, 3:10 AM
Sneha KPosted Mar 22, 2016, 2:49 AM
Saillesh PawarPosted Mar 22, 2016, 2:38 AM
Sneha KPosted Mar 22, 2016, 2:36 AM
Saillesh PawarPosted Mar 22, 2016, 2:32 AM
Sneha KPosted Mar 22, 2016, 2:19 AM
Saillesh PawarPosted Mar 22, 2016, 2:04 AM
Pradeep SahooPosted Mar 22, 2016, 1:34 AM