【问题标题】:Saving checked fields, like in Online Shop保存选中的字段,如在线商店
【发布时间】:2015-09-29 14:38:49
【问题描述】:

我正在尝试做结帐之类的事情。在网上商店。我正在从数据库中获取列表,我想将选定的列表发送到其他视图。

 @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <fieldset>
            <div class="clstable">
                <table id="table" border="0" style="width:60%">
                  <tbody>
                    @foreach (var item in Model)
                        {
                            <tr>                            
                                <td name="Sub">@item.Name</td>                           
                                <td>
                                    <input name="Nam" type="number" min="0" width="10" />
                                </td>
                                <td align="center"><input name="Check" type="checkbox" padding-left 5px; /></td>
                            </tr>
                        }
                    </tbody>
                </table>
               <button type="submit" aligh="center">Submit/Buy</button>
            </div>
        </fieldset>

 public class DBData
    {            
        public string Name { get; set; }
        public string Subscription { get; set; }
    }

[HttpGet]
        public ActionResult About()
        {
            var subscriptions = new List<DBData>();
            using (var db = new OnePortalVsEntities())
            {
              ...
            }
            return View(subscriptions);
        }

如何将选定的 Subs 和它们的编号(在输入编号中选择)发送到其他视图? 我应该创建一个会话还是以某种方式将所有内容发布到控制器?

【问题讨论】:

  • 查看编辑器而不是输入 - 然后在发布后您将在模型中拥有它
  • 你需要展示你的模型。您当前的实现只是创建具有重复 name 属性(没有索引器)的输入和复选框,因此在提交表单时与您的模型完全没有关系。
  • 至少您的模型(视图模型)需要(例如)bool IsSelectedint Number 属性。然后您需要在视图中使用for 循环 - for(int i = 0; i &lt; Model.Count; i++) { @Html.EditorFor(m =&gt; m[i].Number) @Html.CheckboxFor(m =&gt; m[i].IsSelected) ..... } 以便将表单控件绑定到模型
  • Tnx 全部解决了问题。

标签: c# .net visual-studio model-view-controller http-post


【解决方案1】:
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    <fieldset>
        <div class="clstable">
            <table id="table" border="0" style="width:60%">
                <tbody>
                @for (int i = 0; i < Model.Count(); i++ )
                {
                    <tr>
                        <td name="Sub">@Model[i].Name</td>
                        <td>
                            <input name="Nam" type="number" min="0" width="10" />
                        </td>
                        <td align="center">@Html.CheckBoxFor(x=>x[i].BoolProperty)</td>
                    </tr>
                }
                </tbody>
            </table>
            <button type="submit" aligh="center">Submit/Buy</button>
        </div>
    </fieldset>
}

你也错过了&lt;tbody&gt;标签。

【讨论】:

    猜你喜欢
    • 2011-11-18
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2014-06-27
    相关资源
    最近更新 更多