javascript - 一键提交多个单选按钮值 ASP.NET MVC

标签 javascript c# html asp.net asp.net-mvc

我的页面上有三组单选按钮,我想用一个按钮提交它们的每个值。

<table>
    <tr>
     <td>
      @Html.RadioButton("rating1", "yes", true) Yes
      @Html.RadioButton("rating1", "no", false) No
      @Html.RadioButton("rating1", "maybe", false) Maybe
     </td>
    </tr>
<tr>
     <td>
      @Html.RadioButton("rating2", "yes", true) Yes
      @Html.RadioButton("rating2", "no", false) No
      @Html.RadioButton("rating2", "maybe", false) Maybe
     </td>
    </tr>
<tr>
     <td>
      @Html.RadioButton("rating3", "yes", true) Yes
      @Html.RadioButton("rating3", "no", false) No
      @Html.RadioButton("rating3", "maybe", false) Maybe
     </td>
    </tr>
</table>

我想将字典作为参数发送到 Controller 操作,以便我可以检索每个评级的值。

Controller 看起来像这样:

public ActionResult Rate(Guid uniqueId, Dictionary<Guid, string> ratings)
        {
            [...]
        }

我已经尝试过:

<input type="submit" value="Send Ratings" onclick="@Url.Action("Rate", "Controller", new {new Dictionary<string,string> {{"rating1", "yes"}, {"rating2", "no"}, {"rating3", "maybe"}})"></input>

但是不允许将字典作为 RouteValue 传递。

如何通过一个按钮/提交将所有 3 个单选按钮 [名称、值] 对发送到操作?另外,这三个组应该采用相同的形式还是单独的形式?

我愿意使用 JavaScript,但更喜欢使用 Razor HTML 帮助程序。

谢谢

最佳答案

型号

public class ExampleViewModel
{
    public ExampleViewModel()
    {
        Answers = new Dictionary<string, string>();
        Questions = new List<KeyValuePair<string, List<string>>>();
    }

    public Dictionary<string, string> Answers { get; set; }
    public List<KeyValuePair<string, List<string>>> Questions { get; set; }
    public ExampleViewModel Add(string key, string[] value)
    {
        Questions.Add(new KeyValuePair<string, List<string>>(key, value.ToList()));
        return this;
    }
}

Controller

    [HttpGet]
    public ActionResult Index()
    {
        ExampleViewModel model = new ExampleViewModel();
        model.Add("rating1",new[] { "Yes" ,"No", "Maybe"});
        model.Add("rating2", new[] { "Yes", "No", "Maybe" });
        model.Add("rating3", new[] { "Yes", "No", "Maybe" });
        return View(model);
    }
    [HttpPost]
    public ActionResult Index(ExampleViewModel model)
    {
        //model.Answers is the dictionary of the values submitted 
        string s = model.Answers.Count.ToString();
        return View();
    }

查看

    @model ExampleViewModel
    @using (Html.BeginForm())
    {
        <table class="table table-bordered table-striped">
            @for(int i=0;i<Model.Questions.Count;i++)
            {
                var question = Model.Questions[i];
                <tr>
                    <td>
                        @foreach (var answer in question.Value)
                        {
                            <input type="hidden" name="Model.Answers[@question.Key].Key" value="@question.Key" />
                            <input type="hidden" name="Model.Answers.Index" value="@question.Key" />
                            @Html.RadioButton("Model.Answers[" + question.Key+"].Value", answer, false) @answer
                        }
                    </td>
                </tr>
            }
            <tr>
                <td>
                    <input type="submit" class="btn btn-primary" value="Submit" />
                </td>
            </tr>
        </table>
    }

model.Answers 将保存包含提交值的字典

关于javascript - 一键提交多个单选按钮值 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28468208/

相关文章:

javascript - 事件传播和冒泡

javascript - 将 <b></b> 标签添加到文本区域中的选定文本

javascript - 在 JavaScript 中使用异步 AJAX 请求管理异步 IndexedDB

c# - 从 2 个表中获取行

javascript - ASP :textbox Uncaught TypeError: Cannot read property 'value' of undefined

html - 将 CSS 应用于页面上的特殊字符

javascript - 创建具有对象属性的变量 - Angular

javascript - 对表格进行排序时,第一列中的按钮消失

javascript - 如何在javascript中将字符一一附加到输入字段而不是使用sth.value() = sth?

c# - Linq 按升序排序 按降序排序