2013-03-22 59 views
0

我有一个视图我想创建/保存,而不是模型使用枚举如下。MVC4从枚举创建视图

public enum RulesEnum 
{ 
    StopAtLight = 1, 
    StopAtStreet, 
    StopAtCrossWalk, 
    WaitAtCrossWalk, 
} 

示范

public class RulesModel 
{ 
    public RulesEnum Rules { get; set; } 
    public string Comment { get; set; } 
    // some other props that you have 
} 

@model RulesModel 
    @using (Html.BeginForm()) { 
     <input type="radio" name="Rules" value="0" id="Rules1_No" /> 
     <input type="radio" name="Rules" value="1" id="Rules1_Yes" /> 
     <label for="Rules1">Stop at Light</label> 
     <input type="radio" name="Rules" value="0" id="Rules2_No" /> 
     <input type="radio" name="Rules" value="1" id="Rules2_Yes" /> 
     <label for="Rules2">Stop at Street</label> 
     <input type="radio" name="Rules" value="0" id="Rules3_No" /> 
     <input type="radio" name="Rules" value="1" id="Rules3_Yes" /> 
     <label for="Rules3">Stop at CrossWalk</label> 
     <input type="text" name="Rules" id="Rules4" /> 
     <label for="Rules4">Wait at CrossWalk</label> 
     <input type="submit" value="Submit" /> 
    } 

控制器动作

[HttpPost] 
public ActionResult TheMethodThatAcceptsTheInput(RulesModel model) 
{ 
    // do as you wish here, I assume you already know what to do here 
    // at this point, if the user selects a radio button 
    // then the model.Rules will show it, as an enum value of course 
    return View(model); 
} 

这上面代码只是证明了每个枚举值将有不同的控制ASSO与它相关。所以基本上我想为每个枚举值定义不同的控件。

我将要创建一个KeyValuePairs的列表,以便我可以将用户为每个枚举值选择的值发送回控制器。然而,凭借我有限的知识,我只知道如何将精确定义的模型发送回控制器。当用户按下提交按钮时,如何构建我的KeyValuePairs列表并将它们发送回控制器?

我已经研究过各种替代方法,如AutoMapper和ViewModels,但我不认为我的具体问题与这些解决方案有关。我是相当新的,任何人都可以提供一些指导解决这个问题的最佳方法?

谢谢!

更新: 看着冯的例子,我修改了我的一点点。唯一的区别是,我不想要枚举的列表(意味着4枚枚举值= 4单选按钮),但我希望每个枚举值是一个问题,例如:

停在光?是/否(2个单选按钮) 停在街上?是/否(2个单选按钮) 停在人行横道?是/否(2个单选按钮) 在人行横道等待? 30秒(文本框)

我不知道该怎么做是为了例如用户点击提交。我需要以某种方式看待每一个问题,并建立列表发送到控制器,所以它看起来像下面这样:

[(StopAtLight, "false"), 
(StopAtStree, "true"), 
(StopAtCrossWalk, "false"), 
(WaitAtCrossWalk, "40")] 

不知道当用户按下提交因为它是期待一个模式,我如何建立这个清单类似于冯发布的内容。

希望这些进一步的信息有助于了解我的情况。感谢你的帮助。

+0

请重新表述您的问题,和/或提供您当前(相关)控制器的操作方法和视图模型或模型。 – Dai 2013-03-22 01:55:18

回答

0

我不确定你想要用KeyValuePairs做什么,但是基于你问题中的标记,你想在页面上显示enum作为选项,然后有一些文本框用于输入,然后将它们提交给你的控制器。如果是这样的话,那么你可以做到以下几点:

示范

public class RulesModel 
{ 
    public bool StopAtLight { get; set; } 
    public bool StopAtStreet { get; set; } 
    public bool StopAtCrossWalk { get; set; } 
    public string WaitAtCrossWalk { get; set; } 
    // some other props that you have 
} 

@model RulesModel 
@using (Html.BeginForm()) { 
    <h1>Stop at Light</h1> 
    <input type="radio" name="StopAtLight" value="false" id="Rules1_No" /> 
    <label for="Rules1_No">No</label> 
    <input type="radio" name="StopAtLight" value="true" id="Rules1_Yes" /> 
    <label for="Rules1_Yes">Yes</label> 
    <h1>Stop at Street</h1> 
    <input type="radio" name="StopAtStreet" value="false" id="Rules2_No" /> 
    <label for="Rules2_No">No</label> 
    <input type="radio" name="StopAtStreet" value="true" id="Rules2_Yes" /> 
    <label for="Rules2_Yes">Yes</label>    
    <h1>Stop at CrossWalk</h1> 
    <input type="radio" name="StopAtCrossWalk" value="false" id="Rules3_No" /> 
    <label for="Rules3_No">No</label> 
    <input type="radio" name="StopAtCrossWalk" value="true" id="Rules3_Yes" /> 
    <label for="Rules3_Yes">Yes</label>    
    <h1>Wait at CrossWalk</h1> 
    <input type="text" name="WaitAtCrossWalk" id="WaitAtCrossWalk" /> 
    <input type="submit" value="Submit" /> 
} 

控制器动作

[HttpPost] 
public ActionResult TheMethodThatAcceptsTheInput(RulesModel model) 
{ 
    // if model.StopAtLight == false then the user selected No otherwise it was Yes 
    // same goes for the rest of the radio buttons 
    return View(model); 
} 
+0

感谢您的输入Von!你基本上对我正在做的事情有正确的想法,对不起,但是这一个很难让我摆脱我的头脑和问题的形式,所以这是我的错。请看我的编辑版本,我用你的例子作为出发点。 – mvcNewbie 2013-03-22 14:11:27

+1

我根据您更新的问题修改了我的答案。你真的很奇怪地使用枚举。 – 2013-03-24 08:50:45