Related
I have 2 columns, the column on the left containing a form and the column on the right containing an iframe which shows a PDF file. I need the PDF as close to the left column as possible in order to fit them both on the page, although no matter what column sizes I use, the left column seems to push the right column out. Can anyone give me a solution to getting both columns side by side?
the edit view:
<div class="container">
<div class="row">
<div class="col-md-10" >
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SecondarySchoolSurvey</h4>
<br />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Id)
<div class="form-group">
#Html.LabelFor(model => model.Q1, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Q1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RollNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RollNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.RollNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.OfficialSchoolName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.OfficialSchoolName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.OfficialSchoolName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CampDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CampDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CampDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SurveyFileName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SurveyFileName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SurveyFileName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FilePage, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FilePage, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FilePage, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q2, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EnumDropDownListFor(model => model.Q2, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Q2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q3, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Q3, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q3, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q4, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Q4, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q4, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q5, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Q5, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q5, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Q6a, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EnumDropDownListFor(model => model.Q6a, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Q6a, "", 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>
<div class="col-md-2" >
<br />
<br />
<br />
<iframe width="700" height="800" src="#Url.Action("OpenPDF", new { id = 8 })"></iframe>
</div>
</div>
</div>
this is what they look like currently(if i make the left column size any less than 10 then the Labels start to shrink):
When I try both columns as col-md-6 this is what I get (if i make the left column size any less than 10 then the Labels start to shrink as you can see). I am not sure what is causing the white space in between the 2 columns:
You need to separate the label in different column. Give label the enough space and reduced the first column width in two main columns (e.g 6 for form and 6 for pdf container). e.g
<div class="form-group">
<div class="row">
<div class="col-md-6" >
#Html.LabelFor(model => model.Q1, htmlAttributes: new { #class = "control-label col-md-2" })
</div>
<div class="col-md-6">
#Html.EditorFor(model => model.Q1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Q1, "", new { #class = "text-danger" })
</div>
</div>
</div>
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);
}
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)
{
}
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.
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;