无法在表单帖子上绑定 html.checkbox

发布于 2024-11-08 03:20:37 字数 1913 浏览 0 评论 0原文

所以我有一个名为 ProductViewModel 的视图模型,其中包含可以生产产品的站点列表。我试图弄清楚如何使用复选框在表单上显示这一点,以便用户选择可以生产产品的站点。看起来很简单,对吧?嗯,它不像我想要/需要的那样工作。希望有人可以帮助指导我采取正确的方法来做到这一点。

我的课程:

 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中看到以下输出:

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

我理解隐藏字段,但我真正需要的是获取所选项目的值。所以我需要拿回迈阿密的名单。我不需要 html 助手似乎想要发送的假/真东西(即迈阿密=真),

所以我尝试了这个。

@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 列表。

操作如下:

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

其他值(ProductName 等...)映射良好。

我做错了什么?我觉得我错过了一些东西,因为由于 MVC 如何简化许多其他表单处理情况,这应该更容易。

提前致谢...

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.

My classes:

 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;}    
 }

Part of my view:

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

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" />

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)                   
}  

And the output is:

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

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.

The action is like this:

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

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

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.

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-11-15 03:20:37

如何使用编辑器模板而不是与循环作斗争:

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

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

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

在相应的编辑器模板 ~/Views/Shared/EditorTemplates/Sites.cshtml 内:

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

现在不仅您的视图代码更加干净而且正确将为输入字段生成名称,以便模型绑定器能够将所选值绑定回 POST 操作中。

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

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" />
}

and inside the corresponding editor template ~/Views/Shared/EditorTemplates/Sites.cshtml:

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

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)
{
    ...
}
弄潮 2024-11-15 03:20:37

这对我有用。

// View Model
[Display(Name="Boolean Property")]
[UIHint("booleancheckbox"]
public bool? booleanProperty;

视图

// View
@Html.EditorFor(m => m.booleanProperty, new { @onclick = "Toggle(this);" })

编辑器模板 - 添加更多代码来处理空值

// Editor Template booleancheckbox.cshtml
@model bool?

@{
    labelText = ViewData.ModelMetadata.DisplayName != null ?
                ViewData.ModelMetadata.DisplayName : 
                ViewData.ModelMetadata.PropertyName;
}

<label for="@ViewData.ModelMetadata.PropertyName">@labelText

    @Html.CheckBox(string.Empty, Model.Value, ViewContext.ViewData)

</label>

Here is what is working for me.

// View Model
[Display(Name="Boolean Property")]
[UIHint("booleancheckbox"]
public bool? booleanProperty;

View

// View
@Html.EditorFor(m => m.booleanProperty, new { @onclick = "Toggle(this);" })

Editor Template - add some more code to handle null values

// Editor Template booleancheckbox.cshtml
@model bool?

@{
    labelText = ViewData.ModelMetadata.DisplayName != null ?
                ViewData.ModelMetadata.DisplayName : 
                ViewData.ModelMetadata.PropertyName;
}

<label for="@ViewData.ModelMetadata.PropertyName">@labelText

    @Html.CheckBox(string.Empty, Model.Value, ViewContext.ViewData)

</label>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文