asp.net-mvc-3 – html.TextBoxFor和html.Textbox,POSTing值,参
好吧,伙计们,需要一些帮助!
我正在使用asp.net mvc3 razor(我相当新,但做了很多网页表单) 好的,这个问题 我的问题围绕提交视图. 我想将模型返回到控制器的HttpPost方法中的参数.基本上做: public ActionResult Personal() { DataModel dataModel = new DataModel(); FormModel model = new FormModel(); model.candidateModel = dataModel.candidateModel; model.lookupModel = new LookupModel(); return View(model); } [HttpPost] public ActionResult Personal(FormModel formModel) { if (ModelState.IsValid) { //stuff } return View(formModel); } 现在… 这工作(意味着我可以看到值)但是很繁琐,因为我必须准确地写出每个字段在字符串中的位置: @Html.TextBox("formModel.candidateModel.tblApplicant.FirstName",Model.candidateModel.tblApplicant.FirstName) 它呈现如下: <input name="formModel.candidateModel.tblApplicant.FirstName" id="formModel_candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/> 这不起作用: @Html.TextBoxFor(c => c.candidateModel.tblApplicant.FirstName) 它呈现如下: <input name="candidateModel.tblApplicant.FirstName" id="candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/> 现在我假设问题在于id的差异 所以请回答我: >我是以正确的方式来做这件事的 快速澄清: 另一方面说明: 谢谢大家的帮助 回答: 在我的视图中,这是一个问题,我在这个答案中用片段替换了所有代码并且它有效. 再次感谢你 解决方法
是.
您的代码中还有其他内容使其无效.由于您没有显示所有代码,因此很难说.这是一个完整的工作示例,它说明并证明您的代码还有其他内容: 模型: public class FormModel { public CandidateModel candidateModel { get; set; } } public class CandidateModel { public Applicant tblApplicant { get; set; } } public class Applicant { public string FirstName { get; set; } } 控制器: public class HomeController : Controller { public ActionResult Index() { return View(new FormModel { candidateModel = new CandidateModel { tblApplicant = new Applicant { FirstName = "fn" } } }); } [HttpPost] public ActionResult Index(FormModel formModel) { // the username will be correctly bound here return View(formModel); } } 视图: @model FormModel @using (Html.BeginForm()) { @Html.EditorFor(c => c.candidateModel.tblApplicant.FirstName) <button type="submit">OK</button> }
我同意TextBoxFor是限制性的.这就是为什么我建议你总是使用EditorFor而不是TextBoxFor.它允许您使用 例如: public class MyViewModel { [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)] public DateTime CreatedAt { get; set; } } 并在视图中: @model MyViewModel @Html.EditorFor(x => x.CreatedAt) 将完全按照您的预期格式化日期. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 数以千计的重定向不好SEO?
- Asp.net MVC4,C#创建对象实例
- 如何在ASP.NET WebService调用中动态初始化文化?
- asp.net-mvc-routing – ASP.NET 4 MVC Web API:复杂路由的
- asp.net-mvc – MvcHtmlString.Create()和Html.Raw()之间的
- ASP.NET将整数绑定到CheckBox的Checked字段
- wcf – OData是否违反了关注点?
- ASP.NET,jQuery,脏窗体和window.onbeforeunload
- asp.net-core-mvc – IN MVC6返回Json(行,JsonRequestBehav
- asp.net – 如何验证动态创建的控件?
- asp.net – 我可以在DefaultModelBinder上禁用Da
- asp.net – 在Cookie中存储多个值
- asp.net – ActionLink为Attribute Route生成错误
- asp.net – 在webapi中实现PATCH
- reportviewer – 在ASP.NET MVC中的RDLC报表中看
- 【译】使用Jwt身份认证保护 Asp.Net Core Web Ap
- 数以百万计的匿名ASP.Net个人资料?
- asp.net-core – 为什么要在ASP.NET Core中使用完
- asp.net-mvc-2 – 如何格式化内联剃刀变量
- asp.net – 操作无法完成. FORMATETC结构无效