【问题标题】:My UI does not reflect changes to my Model [duplicate]我的 UI 没有反映对我的模型的更改 [重复]
【发布时间】:2016-05-17 11:15:31
【问题描述】:

我有一个 ASP MVC Web 应用程序。

我有一个部分表单,它最初显示“Hello Andy!”。 我按下提交按钮,然后将其更改为“Hello Andy Again!”。 我将模型传回 UI。 标签仍显示旧值。

为什么?

我的标记:

@using (Ajax.BeginForm("SaveAlertPreferences", "Users", new AjaxOptions
{
    UpdateTargetId = "partialform",
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "POST",
{
    <div>
        @Html.AntiForgeryToken()
        <div class="section group">
            <div class="col span_3_of_12">
                @Html.LabelFor(model => model.myStub)
            </div>
            <div class="col span_9_of_12">
                @Html.TextBoxFor(model => model.myStub)
            </div>
        </div>

        <div class="section group">
            <div class="col span_3_of_12">
            </div>
            <div class="col span_4_of_12">
                <input type="submit" value="Press me" />
            </div>
            <div class="col span_5_of_12">
            </div>
        </div>
    </div>
}

我的模特:

public class ChangeAlertPreferencesModel
{
    public string myStub { get; set; }
}

我的控制器:

[AcceptVerbs("HEAD", "GET")]
public PartialViewResult _ChangeAlertPreferences()
{
    Response.CacheControl = "no-cache";

    ChangeAlertPreferencesModel m = new ChangeAlertPreferencesModel();
    m.myStub = "Hello Andy!";
    return PartialView("_ChangeAlertPreferences", m); 
}

[HttpPost]
public PartialViewResult SaveAlertPreferences(ChangeAlertPreferencesModel m)
{
    Response.CacheControl = "no-cache";
    if (ModelState.IsValid)
    {
        m.myStub = "Hello Andy Again!";
        return PartialView("_ChangeAlertPreferences", m);
    }
    else
    {
        m.myStub = "I have errored!";
        return PartialView("_ChangeAlertPreferences", m);
    }
    return null;
}

【问题讨论】:

  • 第一个:partialform 是标签父级 div 的 id 吗?第二:如果您在项目中没有引用 jquery-ajax-unobtrusive.js,则 ajax 表单和其他 ajax 功能“行为不端”,因此请确保您明白这一点。你可以通过 NuGet 下载它。
  • @Spluf 嗨,感谢您的宝贵时间。是的,partialform 是表单父级的 id。我会看看那个框架 - 谢谢
  • @CodeCaster 谢谢。会看看
  • 这是因为HtmlHelper 方法使用来自ModelState 的值而不是模型属性。 this answer 的第二部分解释了这种行为
  • @StephenMuecke 很酷- 谢谢

标签: asp.net asp.net-mvc


【解决方案1】:

为了完成我的评论,您的表单语句看起来很奇怪,所以您可能想试试这个:

@using (Ajax.BeginForm("SaveAlertPreferences", "Users", new AjaxOptions
{
    UpdateTargetId = "partialform",
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "POST",
}))
{

        @Html.AntiForgeryToken()
        <div class="section group">
            <div class="col span_3_of_12">
                @Html.LabelFor(model => model.myStub)
            </div>
            <div class="col span_9_of_12">
                @Html.TextBoxFor(model => model.myStub)
            </div>
        </div>

        <div class="section group">
            <div class="col span_3_of_12">
            </div>
            <div class="col span_4_of_12">
                <input type="submit" value="Press me" />
            </div>
            <div class="col span_5_of_12">
            </div>
        </div>
}

为了解释,在第一行中,using 部分声明了表单及其行为方式,然后您结束(关闭)该声明并打开应包含表单主体的块。

【讨论】:

  • 谢谢。我不得不从 using 位中省略几行代码作为我不允许发布的公司代码。模型正在改变值,因此它与缓存问题有关,也许您建议的框架会有所帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多