asp.net-mvc-3 - 如何在mvc 3中获取标签值?

标签 asp.net-mvc-3 razor

在模型中:

 public class DataViewModel
{
    public IList<RowsCollection> Rows { get; set; }

    public PaiementMethod PaiementMethod { get; set; }

}
public class RowsCollection
{
    public string ID { get; set; }  
    public string Question { get; set; }
}

public enum PaiementMethod
{
    Cash,
    CreditCard,
}

Controller - 索引ActionResult中,我返回了我的模型并将其渲染到 View 页面中,如下所示:

      @model WebNexpo.Controllers.DataViewModel
     @using (Html.BeginForm("Save", "page", FormMethod.Post, new { id = "saves"}))
      {
        foreach (var item in Model.Rows)
        {
           @Html.Label(item.Question)
           <label for="paiement_cash">
            Cash</label>
        @Html.RadioButtonFor(m => m.PaiementMethod, "Cash", new { name = "paiement_cash" + @item.ID + "", id = "radioID" + @item.ID + "", Group = "Group" + @item.ID + "" })
        <label for="paiement_cc">
            Credit card</label>
         @Html.RadioButtonFor(m => m.PaiementMethod, "CreditCard", new { name = "paiement_cc" + @item.ID + "", id = "radioname" + @item.ID + "", Group = "Group" + @item.ID + "" })

         }
       <input id="Saveser" type="submit" value="" class="button1" />
   }

在提交表单操作事件中:

     [HttpPost]
    public ActionResult Save(DataViewModel model)
    {
        if (ModelState.IsValid)
        {
            //want to read all the label which selected rediobutton.
              means suppose 4 question render on page and user have selected only 2
             question of the answer.so How can accomplish here?  
        }
        return RedirectToAction("Index", new {value = "123"});
    }

最佳答案

这里有一些不一致的地方。您正在为每行渲染 2 个单选按钮,因此您可能希望将 View 模型重新组织为:

public class DataViewModel
{
    public IList<RowsCollection> Rows { get; set; }
}

public class RowsCollection
{
    public string ID { get; set; }  
    public string Question { get; set; }
    public PaiementMethod PaiementMethod { get; set; }
}

然后:

@model DataViewModel
@using (Html.BeginForm("Save", "page", FormMethod.Post, new { id = "saves"}))
{
    for (var i = 0; i < Model.Rows.Count; i++)
    {
        <div>
            @Html.HiddenFor(x => x.Rows[i].ID)
            @Html.LabelFor(x => x.Rows[i].Question)
            @Html.EditorFor(x => x.Rows[i].Question)

            @Html.Label("payment_cash" + i, "Cash")
            @Html.RadioButtonFor(m => m.Rows[i].PaiementMethod, "Cash", new { id = "payment_cash" + i })

            @Html.Label("payment_cc" + i, "Credit card")
            @Html.RadioButtonFor(m => m.Rows[i].PaiementMethod, "CreditCard", new { id = "payment_cc" + i })
        </div>
    }
    <input id="Saveser" type="submit" value="" class="button1" />
}

最后:

[HttpPost]
public ActionResult Save(DataViewModel model)
{
    if (ModelState.IsValid)
    {
        // model.Rows[0].PaiementMethod will contain the selected payment method for the first question
        // model.Rows[1].PaiementMethod will contain the selected payment method for the second question
        // ...
    }
    return RedirectToAction("Index", new { value = "123" });
}

或者,如果您想要单一付款方式,您可以保持 View 模型不变,但然后将单选按钮保留在 View 中的循环之外。像这样:

@using (Html.BeginForm("Save", "page", FormMethod.Post, new { id = "saves" }))
{
    for (var i = 0; i < Model.Rows.Count; i++)
    {
        <div>
            @Html.HiddenFor(x => x.Rows[i].ID)
            @Html.LabelFor(x => x.Rows[i].Question)
            @Html.EditorFor(x => x.Rows[i].Question)
        </div>
    }

    @Html.Label("payment_cash", "Cash")
    @Html.RadioButtonFor(x => x.PaiementMethod, "Cash", new { id = "payment_cash" })

    @Html.Label("payment_cc", "Credit card")
    @Html.RadioButtonFor(x => x.PaiementMethod, "CreditCard", new { id = "payment_cc" })

    <input id="Saveser" type="submit" value="" class="button1" />
}

关于asp.net-mvc-3 - 如何在mvc 3中获取标签值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9747629/

相关文章:

c# - 在 View 中创建新的 SelectList 时未设置 DropDownListFor 值

javascript - 使用 Javascript 重新加载 HTML Img 标签

asp.net-mvc-3 - 如何防止ASP.NET MVC应用程序中的DoS攻击?

asp.net-mvc - Razor - 在没有 Render() 和没有编码的情况下渲染

c# - Razor 语法在 MVC 5 View VS2013 上损坏

asp.net-mvc-3 - 在 html 标签内添加 html 输入

c# - 处理 session 超时数据丢失

asp.net-mvc-3 - ASP.Net MVC 3 JSON 模型绑定(bind)和服务器端模型验证与客户端验证混合

c# - 在 ASP.NET 5 中将 Razor View 呈现为字符串

visual-studio-2010 - Visual Studio 2010 陷入 MVC3 Razor 和 Azure 的解决方案