MVC - post视图模型没有被填充
本文关键字:填充 模型 post 视图 MVC | 更新日期: 2023-09-27 18:01:51
操作方法:
[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}";
}
}
}
viewmodel on post全部为空。如果我使用实体框架生成的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>
}
如果您有字段而不是属性,则需要更改视图模型定义以拥有如下属性:
public class RegisterViewModel
{
public string Title { get; set;}
public string FirstName { get; set;}
//............
//............
}
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
}