MVC Bind包括不工作 [英] MVC Bind Include not working
问题描述
我有以下课程
公共类JuryInfoModel
{
public String XKey {get;组; }
public Login LoginInfo {get;组; }
public联系ContactInfo {get;组; }
public JuryInfoModel()
{
LoginInfo = new Login();
ContactInfo = new Contact();
ContactInfo.IsPrimaryMailAddress = true;
XKey ="" ;;
}
}
公共类登录
{
[显示(姓名=" Juror ID号码",ShortName =" Juror ID#" ;),
范围(9999999,int.MaxValue,ErrorMessage ="无效的陪审团ID"),
必需(AllowEmptyStrings = false,ErrorMessage =" * Juror ID号码是必需的")]
public int JurorNumber {get;组; }
[显示(名称="出生日期",ShortName =" DOB"),
DataType(DataType.Date),
DisplayFormat(ApplyFormatInEditMode = true, DataFormatString =" {0d}"),
必需(AllowEmptyStrings = false,ErrorMessage =" *出生日期需要")"
public DateTime DOB {get;组; }
public bool IsValid {get;组; }
}
公共类联系
{
[Display(Name =" First Name"),MaxLength(50),Required(AllowEmptyStrings = false,ErrorMessage) ="名字是必需的');)
公共字符串FirstName {get;组; }
[Display(Name =" Last Name"),MaxLength(50),Required(AllowEmptyStrings = false,ErrorMessage =" Last name is required))
public string LastName {get;组; }
[Display(Name =" Address Line"),MaxLength(150),Required(AllowEmptyStrings = false)]
public string PrimaryAddressLine {get;组; }}
}
我试过用
[HttpPost,ValidateAntiForgeryToken]
public ActionResult CT_00([Bind(Include =" ContactInfo.LastName,ContactInfo.FirstName" ;)] JuryInfoModel模型,String mode = null)
{
// if(TryUpdateModel(model))
// {
//返回视图(模型);
//}
if(TryUpdateModel(model.ContactInfo," ContactInfo",includeProperties:new [] {" LastName"," FirstName"}))
{
JuryInfoModel currentModel = Session [" JUROR"] as JuryInfoModel;
currentModel.ContactInfo.LastName = model.ContactInfo.LastName;
currentModel.ContactInfo.FirstName = model.ContactInfo.FirstName;
Session [" JUROR"] = currentModel;
if(mode ==" edit")
{
return RedirectToAction(" CT_99"," Contact");
}
返回RedirectToAction(" CT_10"," Contact");
}
else
{
return View(model);
}
}
- 但是TryUpdateModel尝试检查尽管我只包含了LastName和FirstName,但还是包含了FirstAddressLine。
- 我试过fiddler 发送PrimaryAddressLine并在服务器端检索一个值。我认为绑定点是仅有效或超过函数定义中包含的内容。为了安全起见,用户不能发送恶意的
数据。
有什么我想念的,因为这会让我感到害怕。
为清楚起见:
- 我只想要服务器接收Bind(Include =)语句中的内容
- 我只想验证TryUpdateModel中的内容
- 我不想创建每个的多个视图模型设计要求的+50或更多页面。
printf(" Love");
你应该发布MVC论坛。
I have the following class
public class JuryInfoModel { public String XKey { get; set; } public Login LoginInfo { get; set; } public Contact ContactInfo { get; set; } public JuryInfoModel() { LoginInfo = new Login(); ContactInfo = new Contact(); ContactInfo.IsPrimaryMailAddress = true; XKey = ""; } } public class Login { [Display(Name = "Juror ID Number", ShortName = "Juror ID #"), Range(9999999, int.MaxValue, ErrorMessage = "Invalid Jury ID"), Required(AllowEmptyStrings = false, ErrorMessage = "*Juror ID number is required")] public int JurorNumber { get; set; } [Display(Name = "Date of Birth", ShortName = "DOB"), DataType(DataType.Date), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0d}"), Required(AllowEmptyStrings = false, ErrorMessage = "*Date of Birth is required")] public DateTime DOB { get; set; } public bool IsValid { get; set; } } public class Contact { [Display(Name = "First Name"), MaxLength(50), Required(AllowEmptyStrings = false, ErrorMessage = "First name is required")] public string FirstName { get; set; } [Display(Name = "Last Name"), MaxLength(50), Required(AllowEmptyStrings = false, ErrorMessage = "Last name is required")] public string LastName { get; set; } [Display(Name = "Address Line"), MaxLength(150), Required(AllowEmptyStrings = false)] public string PrimaryAddressLine { get; set; }
} }
I've tried to use
[HttpPost, ValidateAntiForgeryToken] public ActionResult CT_00([Bind(Include = "ContactInfo.LastName,ContactInfo.FirstName")] JuryInfoModel model, String mode = null) { //if (TryUpdateModel(model)) //{ // return View(model); //} if (TryUpdateModel(model.ContactInfo, "ContactInfo", includeProperties: new[] { "LastName", "FirstName" })) { JuryInfoModel currentModel = Session["JUROR"] as JuryInfoModel; currentModel.ContactInfo.LastName = model.ContactInfo.LastName; currentModel.ContactInfo.FirstName = model.ContactInfo.FirstName; Session["JUROR"] = currentModel; if (mode == "edit") { return RedirectToAction("CT_99", "Contact"); } return RedirectToAction("CT_10", "Contact"); } else { return View(model); } }
- However the TryUpdateModel tries to check on the PrimaryAddressLine even though I've only included LastName and FirstName....
- Ive tried on fiddler to send the PrimaryAddressLine and retrieved a value on the server side. I thought the point of bind was to only valid or past what is included in the function definition. For security sake cant have a user send malicious data.
Is there something I'm missing because this killing me.
For clarity:
- I only want the server side to receive what is in the Bind(Include=) statement
- I only want to validate what is in the TryUpdateModel
- I do not want to create multiple view models of each of the +50 or more pages which are required by design.
printf("Love");
You should post the MVC forum.
这篇关于MVC Bind包括不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!