C# MVC-后期视图模型未填充
行动方法:C# MVC-后期视图模型未填充,c#,asp.net-mvc,entity-framework-6,C#,Asp.net Mvc,Entity Framework 6,行动方法: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Register(RegisterViewModel blahblah) { HttpPostedFileBase uploadFile; if (ModelState.IsValid) { if (blahblah != null) {
[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全部为空。如果我使用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>
@model.ViewModel.RegisterViewModel
@{
ViewBag.Title=“注册新用户”;
}
@使用(Html.BeginForm(“Register”、“Home”、FormMethod.Post))
{
@Html.AntiForgeryToken()
个人资料
@LabelFor(model=>model.Title,“Title”,htmlAttributes:new{@class=“controllabel”})
先生
夫人
博士
错过
@LabelFor(model=>model.FirstName,“First Name”,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}})
@LabelFor(model=>model.LastName,“LastName”,htmlAttributes:new{@class=“controllabel”})
@EditorFor(model=>model.LastName,new{htmlAttributes=new{@class=“form control”}})
@LabelFor(model=>model.Address,“Address”,htmlAttributes:new{@class=“controllabel”})
@EditorFor(model=>model.Address,new{htmlAttributes=new{@class=“form control”})
@LabelFor(model=>model.郊区,“郊区”,htmlAttributes:new{@class=“controllabel”})
@EditorFor(model=>model.substrate,new{htmlAttributes=new{@class=“form control”})
@LabelFor(model=>model.HomePhone,“HomePhone”,htmlAttributes:new{@class=“controllabel”})
@EditorFor(model=>model.HomePhone,new{htmlAttributes=new{@class=“form control”}})
@LabelFor(model=>model.Mobile,“Mobile”,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.Mobile,new{htmlAttributes=new{@class=“form control”})
@LabelFor(model=>model.Email,“Email”,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.Email,new{htmlAttributes=new{@class=“form control”})
紧急情况详情
@LabelFor(model=>model.EmergencyContactName,“Name”,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.EmergencyContactName,new{htmlAttributes=new{@class=“form control”}})
@LabelFor(model=>model.EmergencyContactRelation,“Relation”,htmlAttributes:new{@class=“controllabel”})
@EditorFor(model=>model.EmergencyContactRelation,new{htmlAttributes=new{@class=“form control”})
@LabelFor(model=>model.EmergencyContactPhone,“Phone”,htmlAttributes:new{@class=“controllabel”})
@EditorFor(model=>model.EmergencyContactPhone,new{htmlAttributes=new{@class=“form control”})
@LabelFor(model=>model.EmergencyContactEmail,“Email”,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.EmergencyContactEmail,new{htmlAttributes=new{@class=“form control”})
驾驶执照详情
@LabelFor(model=>model.drivingLicenseNo,“许可证号”,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.drivingliceno,new{htmlAttributes=new{@class=“form control”})
@LabelFor(model=>model.drivingLicenseExpiryDate,“到期日”,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.drivingLicenseExpiryDate,new{htmlAttributes=new{@class=“form control”})
@LabelFor(model=>model.drivingLicenseImage,“许可证图像”,htmlAttributes:new{@class=“control label”})
@LabelFor(model=>model.DateofBirth,“出生日期”,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.DateofBirth,new{htmlAttributes=new{@class=“form control”}})
笔记
发票联
public class RegisterViewModel
{
public string Title { get; set;}
public string FirstName { get; set;}
//............
//............
}
public class RegisterViewModel
{
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
// Others omitted for brevity
}