MVC 5 Data was not pass to mode from view to controller - c#

I have a productModelView which contains 2 Model.All the data in the product and inventory fields was not populated to the model when pass back to the model in the controller.
public class ProductDetails
{
public ProductsTb Product { get; set; }
public InventoryTb ProductInventory { get; set; }
public List<ItemTypeLibrary> ProductTypeList { get; set; }
}
To make code short.i will just show 1 attribute which is inside the productsTB
public ActionResult Create()
{//load up create page to populate product type list
ProductDetails pd = new ProductDetails();
pd.ProductTypeList = db.ItemTypeLibraries.ToList();
return View(pd);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "productID,ArticleID,ProductType,ProductName")] ProductDetails productsTb)
{
if (ModelState.IsValid)
{
db.ProductsTbs.Add(productsTb.Product);
db.SaveChanges();
db.InventoryTbs.Add(productsTb.ProductInventory);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(productsTb);
}
#model RicmasNet.Models.Product.ProductDetails
Create
#model RicmasNet.Models.Product.ProductDetails
#using (Html.BeginForm()) {
<div class="form-horizontal">
<h4>ProductsTb</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Product.ArticleID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ArticleID, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product.ArticleID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductType, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model =>model.Product.ProductType, new SelectList(Model.ProductTypeList,"ItemTypeId","ItemTypeName"), "Select a Type...", new { #style = "width:500px" })
#* #Html.EditorFor(model => model.ProductType, new { htmlAttributes = new { #class = "form-control" } })*#
#Html.ValidationMessageFor(model => model.ProductTypeList, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product.ProductName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductLocation, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductLocation, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product.ProductLocation, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductBrand, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductBrand, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product.ProductBrand, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductOrigin, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductOrigin, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product.ProductOrigin, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductCost, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductCost, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product.ProductCost, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductRetailPrice, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductRetailPrice, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product.ProductRetailPrice, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductDealerPrice, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductDealerPrice, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product.ProductDealerPrice, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductWeight, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductWeight, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductDescEnglish, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductDescEnglish, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductDescNonEnglish, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductDescNonEnglish, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductPicPath, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductPicPath, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
#Html.HiddenFor(model => model.Product.IsDelete)
#Model.Product.IsDelete == true;
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductPicPath2, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductPicPath2, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductPicPath3, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductPicPath3, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.ProductVideo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.ProductVideo, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.COC, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.COC, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Product.COC, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.COCIssue, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.COCIssue, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.COCExpireDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.COCExpireDate, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.COCType, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.COCType, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.COCRF, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.Product.COCRF)
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product.EAN, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Product.EAN, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>}

It's not clear from what you posted as to what isn't being bound, but if you are expecting the entire viewmodel, then remove the Bind(Include...) attribute. It's not needed in that case.

Another option is, use FormCollection instead of passing the model, in case of custom attributes to include object.
[HttpPost]
public ActionResult Create(FormCollection form)
{
// here you can retrieve attributes from form and assign to your object
productsTb.Id = form["Id"]; // it should match the name of tag in View
return View(productsTb);
}

Related

getting my form to autofill when editing using

very new to mvc. My problem is when I have a record I want to edit my view does not autofill the form. Specifically I am trying to enter a bid on an auction site. I want everything else to stay the same and only update the bid. if my form doesn't autofill then everything would be null. any help would be appreciated.
this is from the CarAuctionController
// GET: CarAuction/Edit/5
public ActionResult Edit(int id= 3)
{
CarList carList = db.CarLists.Find(id);
return View();
}
// POST: CarAuction/Edit/5
[HttpPost]
public ActionResult Edit(CarList carlist)
{
try
{
// TODO: Add update logic here
if (ModelState.IsValid)
{
db.Entry(carlist).State=EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View("Index");
}
catch
{
return View();
}
}
this is from the view Edit.cshtml
#model ClassicCarAuction.CarList
#{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CarList</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.ID)
<div class="form-group">
#Html.LabelFor(model => model.ModelYear, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ModelYear, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ModelYear, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Make, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Make, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Make, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Model, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Model, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Model, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CarImage, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CarImage, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CarImage, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DatePosted, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DatePosted, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DatePosted, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AuctionEndDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AuctionEndDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AuctionEndDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ReserveBid, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ReserveBid, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ReserveBid, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PosterUserID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PosterUserID, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PosterUserID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ViewingLocation, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ViewingLocation, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ViewingLocation, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.HighestBid, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.HighestBid, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.HighestBid, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.HighBidUserId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.HighBidUserId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.HighBidUserId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Status, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Status, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Status, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
The problem is that you returning "view", but you do not supply the "model" in which the engine can bind them together, thus the default is null.
return View("Index");
should be
return View("Index", carlist);
You are not passing the retrieved carlist model to the view. Therefore the view does not have access to the model and is not able to fill in the form.
// GET: CarAuction/Edit/5
public ActionResult Edit(int id= 3)
{
CarList carList = db.CarLists.Find(id);
// Change this:
return View(carList);
}

c# values not changing when editing

I'm using asp.net MVC4, whenever I try to change values in the edit page I get this error message
this is my controller`
public ActionResult Edit( Maison maison)
{
if (ModelState.IsValid)
{
ms.Update(maison);
ms.Commit();
return RedirectToAction("Index");
}
return View(maison);
}
this is my HTML
<div class="center">
<div class="container">
<div class="row">
<div class="site site--main">
<header class="site__header">
<h1 class="site__title">Modifier votre annonce</h1>
</header>
<div class="site__panel"><span class="site__header-text">Veuillez remplir les champs avec les informations nécessaires, tout les champs avec * sont obligatoire.</span></div>
<div class="site__main">
<form method="post" action="#Url.Action("Edit","Maisons" , FormMethod.Post)" class="form form--flex form--property-add js-form js-form-property">
#Html.AntiForgeryToken()
<div class="widget js-widget widget--main widget--no-margin widget--no-border widget--collapse">
<div class="widget__header">
<h2 class="widget__title">Informations de base</h2>
</div>
<div class="widget__content">
<div class="row">
<div class="form-group">
#Html.LabelFor(model => model.Titre, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Titre, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Titre, "", new { #class = "text-danger" })
</div>
<div class="form-group">
#Html.LabelFor(model => model.Tutile, htmlAttributes: new { #class = "control-label ", #type = "required" })
#Html.EnumDropDownListFor(model => model.Tutile, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Tutile, "", new { #class = "text-danger" })
</div>
<div class="form-group">
#Html.LabelFor(model => model.type, htmlAttributes: new { #class = "control-label col-md-2" })
#Html.EnumDropDownListFor(model => model.type, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.type, "", new { #class = "text-danger" })
</div>
<div class="form-group">
#Html.LabelFor(model => model.Dimension, htmlAttributes: new { #class = "control-label " })
#Html.EditorFor(model => model.Dimension, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Dimension, "", new { #class = "text-danger" })
</div>
<div class="form-group">
#Html.LabelFor(model => model.Ville, htmlAttributes: new { #class = "control-label " })
#Html.EnumDropDownListFor(model => model.Ville, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Ville, "", new { #class = "text-danger" })
</div>
<!-- end of block .form-property__control-->
<div class="form-group">
#Html.LabelFor(model => model.prix, htmlAttributes: new { #class = "control-label " })
#Html.EditorFor(model => model.prix, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.prix, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="widget js-widget widget--main widget--no-margin widget--no-border widget--collapse">
<div class="widget__header">
<h2 class="widget__title">Plus d'Information</h2>
</div>
<div class="widget__content">
<div class="row">
<div class="form-group">
#Html.LabelFor(model => model.nbrchambre, htmlAttributes: new { #class = "control-label col-md-2" })
#Html.EditorFor(model => model.nbrchambre, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.nbrchambre, "", new { #class = "text-danger" })
<!-- end of block .form-property__control-->
</div>
<div class="form-group">
#Html.LabelFor(model => model.NbrBathRooms, htmlAttributes: new { #class = "control-label " })
#Html.EditorFor(model => model.NbrBathRooms, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.NbrBathRooms, "", new { #class = "text-danger" })
</div>
<div class="form-group">
#Html.LabelFor(model => model.NbrGarages, htmlAttributes: new { #class = "control-label " })
#Html.EditorFor(model => model.NbrGarages, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.NbrGarages, "", new { #class = "text-danger" })
</div>
<div class="form-group form-group--col-12">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "control-label " })
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<button class="form__submit">Modifier</button>
</div>
</form>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
I tried changing button to input and tried using onlick, I'm not sure if the problem is with the method or with html, I'm kinda new to coding if there's more information I need to post please let me know.

How to pass a value of class from view to controller in ASP.Net MVC

I can retrieve a string in this but when I try retrieving a class I'm having an exception error.
Customer = new Customer()
{
FirstName = Request.Form["FirstName"],
LastName = Request.Form["LastName "],
BillingAddress = new Address
{
StreetAddress1 = Request.Form["StreetAddress1"],
}
I already tried setting a value, it saves successfully, but when I'm getting the data from the value exception error occurs.By the way I'm using a model from the SDK. Thank you
View form
#model WebApplication16.Model.ParentModel
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ParentModel</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
<div class="form-group">
#Html.LabelFor(model => model.Customer.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Customer.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Customer.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Customer.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Customer.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Customer.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Customer.BillingAddress.StreetAddress1, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Customer.BillingAddress.StreetAddress1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Customer.BillingAddress.StreetAddress1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Customer.BillingAddress.StreetAddress2, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Customer.BillingAddress.StreetAddress2, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Customer.BillingAddress.StreetAddress2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Customer.BillingAddress.City, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Customer.BillingAddress.City, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Customer.BillingAddress.City, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Customer.BillingAddress.StateCode, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Customer.BillingAddress.StateCode, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Customer.BillingAddress.StateCode, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Customer.BillingAddress.Country, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Customer.BillingAddress.Country, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Customer.BillingAddress.Country, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Customer.BillingAddress.ZipCode, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Customer.BillingAddress.ZipCode, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Customer.BillingAddress.ZipCode, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CreditCard.CreditCardNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CreditCard.CreditCardNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CreditCard.CreditCardNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CreditCard.ExpirationDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CreditCard.ExpirationDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CreditCard.ExpirationDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CreditCard.Issuer, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CreditCard.Issuer, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CreditCard.Issuer, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Payment.Amount, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Payment.Amount, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Payment.Amount, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Payment.Cvv, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Payment.Cvv, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Payment.Cvv, "", new { #class = "text-danger" })
</div>
</div>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Simple answer: You can't send complex datatypes in a form.
For your given example, you can just send "StreetAddress1" as a string (alongside with "FirstName" and "LastName") and build the Customer object in your controller.
Code for your view:
#model Customer
<div>
#using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
#Html.TextBoxFor(m => m.FirstName)<br />
#Html.TextBoxFor(m => m.LastName)<br />
#Html.TextBoxFor(m => m.BillingAddress)<br />
<button type="submit">Save</button>
}
</div>
If you follow this approach, MVC already serializes the Customer object for you:
[HttpPost]
public ActionResult Action(Customer c)
{
string FullName = c.FirstName + " " + c.LastName
}
Use FormCollection Class in You Controller .. This Will Get You All The Posted Values From Use .. Then u Can Extract Values By Key FromCollection Keys .. Create New Object Based On The Value You Got From The FormCollection , Simply Add a FromCollection Parameter
public ActionResult (FormCollection F)
{
}

MVC5 - View does not return the Id of my object on post

My view is shown below:
#model KtembRegistry.Models.Entities.Member
#{
ViewBag.Title = "Üye Güncelleme";
}
<h2>#ViewBag.Title</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.MemberId)
#Html.HiddenFor(model => model.Director.EmployeeId);
<div class="form-horizontal">
<hr />
<div class="form-group">
<input type="button" value="Back to List" onclick="location.href='#Url.Action("Index", "Members")'" class="btn btn-primary" />
<input type="submit" value="Save" class="btn btn-success" />
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.MemberName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.MemberName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.MemberName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Location, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Location, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Location, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.MemberClass, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.MemberClass, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.MemberClass, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ClassId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ClassId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ClassId, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.LastApprovalDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastApprovalDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastApprovalDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TaxNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.TaxNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TaxNo, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Phone, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Phone, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Phone, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Fax, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Fax, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Fax, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Mobile, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Mobile, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Address, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Address, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Address, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<br />
<h4>Direktör</h4>
<hr />
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Director.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Director.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.LastName, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Director.IdentityNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.IdentityNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.IdentityNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Director.Responsibility, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Director.Responsibility, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Director.Responsibility, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<br />
<h3> Personel </h3>
<hr />
<table class="table table-hover">
<thead>
<tr>
<th>İsim</th>
<th>Kimlik No</th>
<th>Yetki</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Employees)
{
<tr>
<td>#item.FirstName</td>
<td>#item.IdentityNo</td>
<td>#item.Responsibility</td>
<td>
#Html.ActionLink("Edit", "Edit", new { #item.EmployeeId }) |
#Html.ActionLink("Delete", "Delete", new { #item.EmployeeId })
</td>
</tr>
}
</tbody>
</table>
</div>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Entity:
public class Employee
{
public virtual int Id { get; protected set; }
public virtual int EmployeeNo { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int IdentityNo { get; set; }
public virtual string Responsibility { get; set; }
public virtual Member Company { get; set; }
}
When I do post, everything is sent to controller just fine except model.Director.EmployeeId. The controller sends the EmployeeId field correctly to the field. Whatever, I do it does not work. Any ideas why my post does not work ?
I have an old habit of making the setter of identity fields in my entities protected in order to protect outside mangling. However, in this case it was causing the identity field from being updated which was the cause of the problem.

Pass data from ViewModel to Controller [duplicate]

This question already has an answer here:
Does the name of parameter have to be model?
(1 answer)
Closed 7 years ago.
I'm trying to post the data of the viewmodel from the form to an action method on the controller, but it's always null.
This is my View:
#model HotelSystem.ViewModels.UpdateReservationVM
#{
ViewBag.Title = "Update Reservation";
}
<h2>Update Reservation</h2>
#using (Html.BeginForm("Update", "Reservations", FormMethod.Post, new { #class = "form-horizontal" }))
{
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(Model => Model.Reservation.Guest.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.HiddenFor(Model => Model.Reservation.ReservationID)
#Html.EditorFor(model => model.Reservation.Guest.FirstName, new { htmlAttributes = new { #class = "form-control", #id = "ReservationID", #disabled = true } })
#Html.ValidationMessageFor(model => model.Reservation.Guest.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.MiddleName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Guest.MiddleName, new { htmlAttributes = new { #class = "form-control", #id = "Arrival", #disabled = true } })
#Html.ValidationMessageFor(model => model.Reservation.Guest.MiddleName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.LastName, htmlAttributes: new { #class = "control-label col-md-2", #id = "Departure" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Guest.LastName, new { htmlAttributes = new { #class = "form-control", #disabled = true } })
#Html.ValidationMessageFor(model => model.Reservation.Guest.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.PhoneNo, "Phone Number", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Guest.PhoneNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.Guest.PhoneNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.MobileNo, "Mobile #", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Guest.MobileNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.Guest.MobileNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.HomeAddress, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Guest.HomeAddress, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.Guest.HomeAddress, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.Occupation, "Occupation", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Guest.Occupation, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.Guest.Occupation, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.VehiclePlateNo, "Vehicle Plate #", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.VehiclePlateNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.VehiclePlateNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.TitleID, "TitleID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("TitleID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.Guest.TitleID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.CountryID, "CountryID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("CountryID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.Guest.CountryID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.NationalityID, "NationalityID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("NationalityID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.Guest.NationalityID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.VIPID, "VIPID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("VIPID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.Guest.VIPID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.AgencyID, "AgencyID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("AgencyID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.AgencyID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.GroupProfileID, "GroupProfileID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("AgencyID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.GroupProfileID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.CompanyID, "Commpany", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("CompanyID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.Guest.CompanyID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.GroupProfileID, "Group Profile", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("GroupProfileID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.GroupProfileID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.SourceID, "SourceID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("SourceID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.SourceID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.EmailAdd, "Email Address", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Guest.EmailAdd, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.Guest.EmailAdd, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Room Information</label>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Arrival, "Arrival", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Arrival, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.Arrival, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Departure, "Departure", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Departure, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.Departure, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Nigt(s)</label>
<div class="col-md-1">
<input class="form-control" id="nights" />
</div>
<label class="control-label col-md-2">No. Of Room</label>
<div class="col-md-1">
#Html.EditorFor(model => model.Reservation.NoOfRooms, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.NoOfRooms, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.NoOfAdults, "No of Adults", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-1">
#Html.EditorFor(model => model.Reservation.NoOfAdults, new { htmlAttributes = new { #class = "form-control", #id = "NoOfAdults" } })
#Html.ValidationMessageFor(model => model.Reservation.NoOfAdults, "", new { #class = "text-danger" })
</div>
#Html.LabelFor(model => model.Reservation.NoOfChild, "Child", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-1">
#Html.EditorFor(model => model.Reservation.NoOfChild, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Reservation.NoOfChild, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Room.RoomTypeID, "RoomTypeID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-2">
#Html.DropDownList("RoomTypeID", null, htmlAttributes: new { #class = "form-control", #id = "RoomTypeID" })
#Html.ValidationMessageFor(model => model.Room.RoomTypeID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.RateID, "Rate", htmlAttributes: new { #class = "control-label col-md-1" })
<div class="col-md-2">
#Html.DropDownList("RateID", null, htmlAttributes: new { #class = "form-control", #id = "RateID" })
#Html.ValidationMessageFor(model => model.Reservation.RateID, "", new { #class = "text-danger" })
</div>
#Html.LabelFor(model => model.Room.RoomID, "RoomID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-1">
#Html.DropDownList("RoomID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Room.RoomID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Rate, "Rate", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Reservation.Rate, new { htmlAttributes = new { #class = "form-control", #id = "Rate" } })
#Html.ValidationMessageFor(model => model.Reservation.Rate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.ReservationTypeID, "ReservationTypeID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("ReservationTypeID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.ReservationTypeID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.GroupProfileID, "GroupProfile", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("GroupProfileID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.GroupProfileID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.TitleID, "TitleID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("TitleID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.Guest.TitleID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Reservation.Guest.NationalityID, "NationalityID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("NationalityID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Reservation.Guest.NationalityID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Payment.PaymentTypeID, "PaymentTypeID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("PaymentTypeID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Payment.PaymentTypeID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Payment.CardID, "CardTypeID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("CardTypeID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Payment.CardID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Payment.BookBy, "BookBy", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("BookBy", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Payment.BookBy, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" onclick="return confirm('Are you sure you want to proceed transaction?');" />
<input type="button" value="Options" class="btn btn-default" id="alert" />
Options
</div>
</div>
</div>
}
Then this is the Action Method:
[HttpPost]
public ActionResult Update(ViewModels.UpdateReservationVM reservation)
{
if (ModelState.IsValid)
{
db.Entry(reservation).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(reservation);
}
Then this is my View Model:
public class UpdateReservationVM
{
public virtual Reservation Reservation { get; set; }
public virtual Payment Payment { get; set; }
public virtual FlightInfo Flight { get; set; }
public virtual RoomReservation Room { get; set; }
public virtual Transfer Transfer { get; set; }
}
What is wrong with this code? Tried to debug it, it always goes to the action method of the controller but values passed are all NULL.
Add an antiforgerytoken in your form:
#using (Html.BeginForm("Update", "Reservations", FormMethod.Post, new { #class = "form-horizontal" }))
{
#Html.AntiForgeryToken()
#*
rest of code
*#
}
Try...
[HttpPost]
public ActionResult Update(ViewModels.UpdateReservationVM reservation)
{
if (ModelState.IsValid)
{
db.Reservation.Attach(UpdateReservationVM.reservation);
db.Entry(UpdateReservationVM.reservation).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(reservation);
}
Check to see that reservation (lowercase) is not null when debugging.
On a side note, you may want to change
public ActionResult Update(ViewModels.UpdateReservationVM reservation)
to
public ActionResult Update(UpdateReservationVM reservation)
and add a using statement at the top of your controller to include the viewmodel namespace...
using YOURPROJECTNAME.NAMEOFVIEWMODELSFOLDER;
So, in my case its
using salesWebsite.viewModel;

Categories