user1490835 user1490835 - 3 months ago 14
C# Question

MVC - On post the view model is not getting populated

action method:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterViewModel blahblah)
{
HttpPostedFileBase uploadFile;
if (ModelState.IsValid)
{
if (blahblah != null)
{
var obj = new tblPersonalDetail()
{
FirstName = blahblah.FirstName,
LastName = blahblah.LastName,
Title = blahblah.Title,
Address = blahblah.Address,
Suburb = blahblah.Suburb,
HomePhone = blahblah.HomePhone,
Mobile = blahblah.Mobile,
Email = blahblah.Email,
EmergencyName = blahblah.EmergencyContactName,
EmergencyPhone = blahblah.EmergencyContactPhone,
EmergencyEmail = blahblah.EmergencyContactEmail,
EmergencyRelation = blahblah.EmergencyContactRelation,
DrivingLicenceExpiryDate = blahblah.DrivingLicenceExpiryDate,
DrivingLicenceNo = blahblah.DrivingLicenceNo,
DateofBirth = blahblah.DateofBirth
};


//if (uploadFile != null && !string.IsNullOrEmpty(uploadFile.FileName))
//{
// uploadFile.SaveAs(Server.MapPath($@"~\Content\Images\{uploadFile.FileName}"));
// obj.ScannedImageLocation = ($@"~\Content\Images\{uploadFile.FileName}");
//}
db.tblPersonalDetails.Add(obj);
db.SaveChanges();
return RedirectToAction("Index");
}
}

return View(blahblah);
}


--registerviewmodel

public class RegisterViewModel
{
public string Title;
public string FirstName;
public string LastName;
public string Address;
public string Suburb;
public string HomePhone;
public string Mobile;
public string Email;
public string EmergencyContactName;
public string EmergencyContactRelation;
public string EmergencyContactPhone;
public string EmergencyContactEmail;
public string DrivingLicenceNo;
// [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime DrivingLicenceExpiryDate;
public string DrivingLicenceImage;
// [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ="{0:yyyy-MM-dd}")]
public DateTime DateofBirth;
public string Notes;
public string NextAppointment;
public string Name
{
get
{
return $"{FirstName} {LastName}";
}
}


}


the viewmodel on post is all null. if i use the tblPersonalDetail model class generated by the entityframework and then change the reference in the view (Register.cshtml), it posts with the data. However not with the custom view model.
--Register.cshtml

@model Leo.ViewModel.RegisterViewModel
@{
ViewBag.Title = "Register New User";
}
@using (Html.BeginForm("Register", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()


<div class="row">
<div class="col-md-8">
<div class="panel">

<div class="panel-heading tabbable" tabindex="0"><h1>Personal Details</h1></div>

<div class="panel-body">
<div class="form-group row">
<div class="col-sm-6">

@Html.LabelFor(model => model.Title, "Title", htmlAttributes: new { @class = "control-label" })
<select class="form-control" id="Title">
<option>Mr</option>
<option>Mrs</option>
<option>Dr</option>
<option>Miss</option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-sm-6">
@Html.LabelFor(model => model.FirstName, "First Name", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="col-sm-6">

@Html.LabelFor(model => model.LastName, "Last Name", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>

<div class="form-group row">
<div class="col-sm-6">
@Html.LabelFor(model => model.Address, "Address", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
</div>

<div class="col-sm-6">
@Html.LabelFor(model => model.Suburb, "Suburb", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Suburb, new { htmlAttributes = new { @class = "form-control" } })
</div>

</div>
<div class="form-group row">
<div class="col-sm-6">
@Html.LabelFor(model => model.HomePhone, "Home Phone", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.HomePhone, new { htmlAttributes = new { @class = "form-control" } })
</div>


<div class="col-sm-6">
@Html.LabelFor(model => model.Mobile, "Mobile", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } })
</div>

</div>
<div class="form-group row">
<div class="col-sm-6">
@Html.LabelFor(model => model.Email, "Email", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
</div>

</div>
</div>

<div class="panel-heading tabbable" tabindex="0"><h1>Emergency Details</h1></div>

<div class="panel-body">

<div class="form-group row">
<div class="col-sm-6">
@Html.LabelFor(model => model.EmergencyContactName, "Name", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.EmergencyContactName, new { htmlAttributes = new { @class = "form-control" } })
</div>

<div class="col-sm-6">
@Html.LabelFor(model => model.EmergencyContactRelation, "Relation", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.EmergencyContactRelation, new { htmlAttributes = new { @class = "form-control" } })
</div>

</div>
<div class="form-group row">
<div class="col-sm-6">
@Html.LabelFor(model => model.EmergencyContactPhone, "Phone", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.EmergencyContactPhone, new { htmlAttributes = new { @class = "form-control" } })
</div>


<div class="col-sm-6">
@Html.LabelFor(model => model.EmergencyContactEmail, "Email", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.EmergencyContactEmail, new { htmlAttributes = new { @class = "form-control" } })
</div>

</div>


</div>

<div class="panel-heading tabbable" tabindex="0"><h1>Driving Licence Details</h1></div>

<div class="panel-body">
<div class="form-group row">
<div class="col-sm-6">
@Html.LabelFor(model => model.DrivingLicenceNo, "Licence No", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.DrivingLicenceNo, new { htmlAttributes = new { @class = "form-control" } })
</div>

<div class="col-sm-6">
@Html.LabelFor(model => model.DrivingLicenceExpiryDate, "Expiry Date", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.DrivingLicenceExpiryDate, new { htmlAttributes = new { @class = "form-control" } })
</div>

</div>
<div class="form-group row">
<div class="col-sm-6">
@Html.LabelFor(model => model.DrivingLicenceImage, "Licence Image", htmlAttributes: new { @class = "control-label" })
<input type="file" name="uploadFile" />

</div>
<div class="col-sm-6">
@Html.LabelFor(model => model.DateofBirth, "Date of Birth", htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.DateofBirth, new { htmlAttributes = new { @class = "form-control" } })
</div>



</div>

</div>
<input type="submit" class="btn btn-primary form-control" value="Submit" />
</div>
</div>

<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">Notes</div>
<div class="panel-body">

<textarea rows="10" cols="15" class="form-control" id="Notes"></textarea>

</div>
</div>
</div>


<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">Invoice</div>
<div class="panel-body">
<div class="form-group row">
<div class="col-sm-6">

<label for="Paid">Paid</label>
<input class="form-control" type="text" id="Paid" placeholder="Amount Paid">
</div>
</div>
<div class="form-group row">
<div class="col-sm-6">

<label for="Balance">Balance</label>
<input class="form-control" type="text" id="Balance" placeholder="Balance">
</div>
</div>
<div class="form-group row">
<div class="col-sm-6">

<label for="Total">Total</label>
<input class="form-control" type="text" id="Total" placeholder="Total Amount">
</div>
</div>
<a href="invoice.html" target="_blank">Print Invoice</a>
</div>
</div>
</div>

</div>


}

Answer

You have fields not properties, you need to change your view model definition to have properties like:

public class RegisterViewModel
 {
    public string Title { get; set;}
    public string FirstName { get; set;}
    //............
    //............
}