【问题标题】:TextBoxFor on model binding not posting modified value模型绑定上的 TextBoxFor 未发布修改后的值
【发布时间】:2015-01-21 17:51:31
【问题描述】:

所以我有这个模型:

public class HoraireChefViewModel
{
    public ChefModel mChef { get; set; }

    public DateTime mDate;

    public int mStartHour;

    public int mEndHour;

    public HoraireChefViewModel()
    {
        mDate = DateTime.Now;
    }
}

当我在控制器上执行获取请求时,我会这样做:

[HttpGet]
public ActionResult GérerHoraireChef(int? chefid)
{
    ChefModel chefToShow = ChefManager.GetChefByID((int)chefid);

    HoraireChefViewModel viewModel = new HoraireChefViewModel
    {
        mChef = chefToShow,
        mEndHour = 0,
        mStartHour = 0
    };

    ModelState.Remove("mEndHour");
    ModelState.Remove("mStartHour");

    return View(viewModel);    
}

[HttpPost]
public ActionResult GérerHoraireChef(HoraireChefViewModel horaireViewModel, string submitButton)
{
    (... some potato)

    return View(horaireViewModel);
}

你可以看到我解决这个问题的许多尝试。

所以在我看来,我渲染的东西是这样的:

@model BoudhaJaune.ViewModels.HoraireChefViewModel

@{
    ViewBag.Title = "Gérer l'horaire de " + Model.mChef.mName;
}

<h2>
    Gérer l'horaire de @Model.mChef.mName
</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    @Html.HiddenFor(item => item.mChef.mChefID)

    <div>
        <div class="float-left">
            @Html.ActionLink("Retourner à la liste", "GérerChef")
        </div>
        <div class="clear"></div>
    </div>
    <div style="background-color: white; border: 1px solid black; border-radius: 5px; padding: 5px; margin: 5px;">
        <div style="width: 100%; display: table; border-spacing: 5px; line-height: 40px">
            <div class="float-left" style="width: 48%">
                <div class="float-left" style="width: 30%">
                    Nom:
                </div>
                <div class="float-right" style="width: 70%">
                    @Html.TextBoxFor(item => item.mChef.mName, new { @style = "width: 300px;", @class = "disabled", disabled = "disabled" })
                    @Html.HiddenFor(item => item.mChef.mName)
                </div>
            </div>
            <div class="float-left" style="width: 48%">
                <div class="float-left" style="width: 30%">
                    Description:
                </div>
                <div class="float-right" style="width: 70%">
                    @Html.TextBoxFor(item => item.mChef.mDescription, new { @class = "disabled", disabled = "disabled", @style = "width: 300px" })
                    @Html.HiddenFor(item => item.mChef.mDescription)
                </div>
            </div>
            <div class="clear"></div>
        </div>
        <div style="width: 100%; display: table; border-spacing: 5px; line-height: 40px">
            <div class="float-left" style="width: 33%">
                <div class="float-left" style="width: 30%">
                    Date:
                </div>
                <div class="float-right" style="width: 70%">
                    @Html.TextBoxFor(item => item.mDate, Model.mDate.ToShortDateString(), new { @class = "datePicker", @title = "Choose a Date"})
                </div>
            </div>
            <div class="float-left" style="width: 33%">
                <div class="float-left" style="width: 30%">
                    Heure de début:
                </div>
                <div class="float-right" style="width: 70%">
                    @Html.TextBoxFor(item => item.mStartHour, new { @class = "positive-integer"})
                </div>
            </div>
            <div class="float-left" style="width: 33%">
                <div class="float-left" style="width: 30%">
                    Heure de fin:
                </div>
                <div class="float-right" style="width: 70%">
                    @Html.TextBoxFor(item => item.mEndHour, new { @class = "positive-integer"})
                </div>
            </div>
            <div class="float-right">
                <input type="submit" value="Ajouter une date" name="submitButton"/>
            </div>
            <div class="clear"></div>
        </div>
        @if (Model.mChef.mListMenuDisponibilités.Count > 0)
        {
            <div style="width: 100%; display: table; border-spacing: 5px; line-height: 40px">
            </div>
        }   
    </div>
}

但是我的主要问题是,由于未知原因,即使我输入 5 或 12,我在 mStartHourmEndHour 属性的 TexBoxFor 字段中输入的数据始终为 0。为什么,以及如何才能解决这个?

编辑

这是为两个有问题的字段生成的 HTML 代码:

<div class="float-left" style="width: 33%">
    <div class="float-left" style="width: 30%">
        Heure de début:
    </div>
    <div class="float-right" style="width: 70%">
        <input class="positive-integer" data-val="true" data-val-number="The field Nullable`1 must be a number." id="mStartHour" name="mStartHour" type="text" value="0" />

    </div>
</div>
<div class="float-left" style="width: 33%">
    <div class="float-left" style="width: 30%">
        Heure de fin:
    </div>
    <div class="float-right" style="width: 70%">
        <input class="positive-integer" data-val="true" data-val-number="The field Nullable`1 must be a number." id="mEndHour" name="mEndHour" type="text" value="0" />

    </div>
</div>

【问题讨论】:

    标签: c# asp.net-mvc model html.textboxfor


    【解决方案1】:

    您的属性需要有 getter 和 setter 才能让 DefaultModelBinder 从发布的值中设置值。

    public class HoraireChefViewModel
    {
      public ChefModel mChef { get; set; }
      public DateTime mDate { get; set; } // change
      public int mStartHour { get; set; } // change
      public int mEndHour { get; set; } // change
      public HoraireChefViewModel()
      {
        mDate = DateTime.Now;
      }
    }
    

    旁注:您无需在 GET 方法中调用 ModelState.Remove(..)(此时尚未向模型状态添加任何内容!)

    【讨论】:

    猜你喜欢
    • 2013-08-30
    • 1970-01-01
    • 2011-07-25
    • 2020-02-26
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    相关资源
    最近更新 更多