加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 该文本区域为null

发布时间:2020-12-15 07:45:40 所属栏目:百科 来源:网络整理
导读:我有一个奇怪的问题,过去几个小时让我很沮丧.我似乎找不到任何相关的东西;也许我不够具体,因为我不确定如何正确地说出来,或者这是一个奇怪的独特问题. 有一个表单,用户填写更新他们的帐户信息,一切正常,除了一个文本区域.一旦表单被POST,这个文本区域(它绑定
我有一个奇怪的问题,过去几个小时让我很沮丧.我似乎找不到任何相关的东西;也许我不够具体,因为我不确定如何正确地说出来,或者这是一个奇怪的独特问题.

有一个表单,用户填写更新他们的帐户信息,一切正常,除了一个文本区域.一旦表单被POST,这个文本区域(它绑定到UserInfo的属性Comments)值就变为null. Comments属性是唯一的null属性.

何时发生
A)没有现有值,用户输入值,属性为空.
B)现有价值,用户做/不改变什么/什么,属性为空.

我只会包含相关代码以保持简洁.希望这就够了.

控制器动作

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.
>在POST操作上抛出异常以证明在发布时值变为null.
>创建了一个具有不同名称的新属性.
>在返回视图之前手动为属性赋值.发布时,该值变为null.
>手动为POST属性赋予属性值,以证明它不是数据库或SQL.这很有效.
>从模型中删除了Fluent Validation属性(如上所述).
>在UserInfo userInfo之前使用[Bind(Prefix =“”)].这并没有改变任何事情.

令我感到沮丧的是,我不得不问:这到底是怎么回事?难道我做错了什么?我必须忽视一些事情.页面上还有另一个文本区域可以正常工作.它只是注释的文本区域,无论条件如何,它始终返回空值.

解决方法

表单被包装如下:
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();

巴姆!它再次起作用.这件事从未发生过,因为我之前没有遇到任何问题.我很高兴这是固定的.我们都会犯错.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读