在复选框MVC呈现在我看来,我的控制器的反应ENUM? [英] Enum with mvc rendering in checkbox on my view, reaction of my controller?

查看:108
本文介绍了在复选框MVC呈现在我看来,我的控制器的反应ENUM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我得到复选框在视图列表,该列表由枚举(标志)来了。如果我的复选框,因为所有相同的名字,为什么我的控制器将在我的视图模型与多重选择全自动更新我的枚举(标志)值?

If i got a list of checkbox in a View, and this list came from Enum (flags). If my checkbox as all the same name, did my controller will update automaticly my Enum (flags) values in my ViewModel with multiple selection ?

假设我在我看来,获得

<% foreach (var t in Enum.GetValues(typeof(FoodType)))
           {
               Response.Write(t.ToString() + " ");
            %>
            <input type="checkbox" name="TypeOfFood"  value="<%:(int)t %>" />

            <% }%>

我的控制器工作,这样

My Controller working like this

public ActionResult Manage(FoodEntity food)
        {


        }

如果我检查许多复选框当我看到那么我foodEntity食物类型的财产,只选择第一个复选框的价值,但我的枚举是一个标志......我需要什么,如果我想支持的标志?

If i check many check box when i look then FoodType property in my foodEntity, only the value of the first checkbox is selected, but my enum is a flag... what i need, if i want support flag ?

感谢。

推荐答案

可惜没有。

这将只是抢了先检查值并分配给你的价值领域。

It will just grab the first checked value and assign that to your value field.

这将是pretty很酷的功能虽然。

That would be a pretty cool feature though.

继承人快速的方法来得到你要寻找的值返回到模型:

Heres a quick way to get the value you're looking for back into your model:

int newEnumValue = Request.Form["CheckBoxField"].Split(',').Aggregate(0, (acc, v) => acc |= Convert.ToInt32(v), acc => acc);

这篇关于在复选框MVC呈现在我看来,我的控制器的反应ENUM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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