不能结合html.checkbox的形式后 [英] Not able to bind html.checkbox on form post

查看:145
本文介绍了不能结合html.checkbox的形式后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我必须有一个在那里的产品可以生产站点的列表视图模型调用ProductViewModel。我试图找出如何使用复选框用户选择该产品可以生产站点表单上显示此。似乎直线前进,对不对?那么它不工作像我想/需要。希望有人能帮助引导我正确的方式做到这一点。

So I have a view model call ProductViewModel which has a list of sites where a product can be produced. I am trying to figure out how to show this on a form using a checkbox for the user to select the sites where the product can be produced. Seems straight forward, right? Well it doesn't work like I want/need. Hoping someone can help guide me to the correct way to do this.

我的类:

 public class ProductViewModel
 {
    public List<Sites> SiteList {get; set;}   
    public string ProductName {get; set;}
    public int ProductId {get; set;}
    public User ProductOwner{get; set;}
 }

 public class Sites
 { 
    public int SiteId {get; set;} 
    public string SiteName {get; set;}
    public bool IsSelected {get; set;}    
 }

我的部分观点:

@Html.LabelFor(m=>m.Sites):
@foreach (var site in Model.Sites)
{
    @Html.CheckBox("Sites", site.IsSelected, new { value = site.SiteName })
    @Html.Label(site.SiteName)
} 

当使用@ Html.Checkbox()我看到在HTML下面的输出从浏览器:

When using @Html.Checkbox() I see the following output in the html from the browser:

<input checked="checked" id="Sites" name="Sites" type="checkbox" value="Miami" />
<input name="Sites" type="hidden" value="false" />

我理解的隐藏字段,但我真正需要的是获得所选项目的价值。所以,我需要回到迈阿密的名单中了。我不需要假/真东西的HTML辅助似乎要送(即迈阿密= TRUE)

I understand the hidden field but what I really need is to get the value for the selected item. So I need to get back the list with Miami in it. I don't need the false/true thing that the html helper seem to want to send (i.e. Miami=true)

所以不是我尝试这样做。

So instead I tried this.

@for(int id=0; id < Model.Sites.Count(); id++)
{
    <input type="checkbox" id="@Model.Sites[id].SiteName" name="Sites[@id]" value="@Model.BoxingSites[id].SiteName" @(Model.Sites[id].IsSelected  ? @"checked=""checked""": "") />
    @Html.Label(Model.Sites[id].SiteName)                   
}  

和输出是:

<input type="checkbox" id="Miami" name="Sites[0]" value="Miami" checked=&quot;checked&quot; />
<label for="Miami">Miami</label>

在这两种情况下,我不能够得到粘结剂张贴到操作时表单值映射到Product.Sites列表。

In both of these cases I am not able to get the binder to map the form values to the Product.Sites list when posting to the action.

的操作是这样的:

[HttpPost]
public ActionResult Edit(ProductViewModel Product)
{
     //Does something with the form data.
}

其他值(产品名称等)地图罚款。

The other values (ProductName etc...) map fine.

我是什么做错了吗?我觉得我失去了一些东西,因为这应该是由于MVC如何简化许多其他形式的处理情况更容易。

What am I doing wrong? I feel I am missing something as this should be easier due to how MVC simplifies so many other form handling situations.

在此先感谢...

推荐答案

有关如何使用编辑模板,而不是与循环挣扎:

How about using an editor template instead of struggling with loops:

@model ProductViewModel
@using (Html.BeginForm())
{
    ... some other form fields

    @Html.LabelFor(x => x.SiteList)
    @Html.EditorFor(x => x.SiteList)

    <input type="submit" value="Create" />
}

和相应的编辑模板中〜/查看/共享/ EditorTemplates / Sites.cshtml

@model Sites
<div>
    @Html.HiddenFor(x => x.SiteId)
    @Html.CheckBoxFor(x => x.IsSelected)
    @Html.LabelFor(x => x.SiteName)
</div>

现在不仅是你的看法code是更加干净的,但正确的名称将用于输入字段生成,使该模型粘结剂将能够选择的值绑定回到了POST操作。

Now not only that your view code is much more clean but proper names will be generated for the input fields so that the model binder will be able to bind the selected values back in the POST action.

[HttpPost]
public ActionResult Create(ProductViewModel model)
{
    ...
}

这篇关于不能结合html.checkbox的形式后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆