c# – 该文本区域为null
我有一个奇怪的问题,过去几个小时让我很沮丧.我似乎找不到任何相关的东西;也许我不够具体,因为我不确定如何正确地说出来,或者这是一个奇怪的独特问题.
有一个表单,用户填写更新他们的帐户信息,一切正常,除了一个文本区域.一旦表单被POST,这个文本区域(它绑定到UserInfo的属性Comments)值就变为null. Comments属性是唯一的null属性. 何时发生 我只会包含相关代码以保持简洁.希望这就够了. 控制器动作 public ActionResult Edit_Information(long id) { // Get user info from the database. // Return the view with the user info from the DB etc. } [HttpPost] public ActionResult Edit_Information(long id,UserInfo userInfo) { if (!this.ModelState.IsValid) { // Invalid return View(userInfo); } // Update the information in the DB. // Redirect the user back to their account. } 剃刀查看HTML <div style="width: 700px; margin-left: auto; margin-right: auto; text-align: left"> @Html.ValidationMessageFor(x => x.Comments) </div> @Html.Partial("~/Views/Shared/_EditorSmiles.cshtml") @Html.TextAreaFor(x => x.Comments,new { @class = "EditorArea profile-comments" }) UserInfo模型 [Validator(typeof(UserInfoValidator))] public class UserInfo { public string Comments { get;set; } } 是的,我在模型上使用FluentValidation.我删除它以查看它是否是原因,但事实并非如此. 我试过的事情 >在POST操作中,我使用了FormCollection formCollection而不是UserInfo userInfo. 令我感到沮丧的是,我不得不问:这到底是怎么回事?难道我做错了什么?我必须忽视一些事情.页面上还有另一个文本区域可以正常工作.它只是注释的文本区域,无论条件如何,它始终返回空值. 解决方法
表单被包装如下:
Html.BeginWindow(); Html.BeginForm("edit_information","user",FormMethod.Post,new { id = "profile" }); <!-- other stuff goes in between here --> Html.EndForm(); Html.EndWindow(); Html.BeginWindow()生成一个包裹在表单周围的表(窗口).这显然导致表单的某些部分无法正确发布. 变成: Html.BeginForm("edit_information",new { id = "profile" }); Html.BeginWindow(); <!-- other stuff goes in between here --> Html.EndWindow(); Html.EndForm(); 巴姆!它再次起作用.这件事从未发生过,因为我之前没有遇到任何问题.我很高兴这是固定的.我们都会犯错. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |