行动方法:
[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}";
}
}
}
post上的viewmodel全为null。如果我使用entityframework生成的tblPersonalDetail模型类,然后更改视图中的引用(Register.cshtml),它会发布数据。但不适用于自定义视图模型。 --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>
}
答案 0 :(得分:3)
您有fields而不是properties,您需要更改您的视图模型定义,使其具有以下属性:
public class RegisterViewModel
{
public string Title { get; set;}
public string FirstName { get; set;}
//............
//............
}
答案 1 :(得分:2)
ASP.NET MVC中的默认模型绑定器仅绑定到可公开访问的属性,但是您当前正在使用不受绑定的字段。
您只需要使用必要的getter / setter来装饰它们,如下所示,使它们成为属性:
public class RegisterViewModel
{
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
// Others omitted for brevity
}