使用编辑器加载所选单选按钮c# [英] Loading selected radio buttons using an editor c#

查看:232
本文介绍了使用编辑器加载所选单选按钮c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个问题:



StackOverflow - 在c#mvc中对单选按钮进行分组

我现在已经管理从表单加载并保存选择的选项到数据库但现在用户可能需要编辑此信息。



如何使用所选用户的选择加载视图?我看不到你可以设置选择答案。在加载时我得到选择的默认值

解决方案

您可以设置 SelectedAnswer GET 操作和您的 EditorTemplate 中的每个课程 $ c>将负责其余的操作。

  public ActionResult Index()
{
var vm = new OrderViewModel();

//下面是DEMO的硬编码。你可以从一些
//其他地方获取数据,并设置课程和选项

var q1 = new Course {ID = 1,Name =Starters};
q1.Options.Add(new Option {ID = 12,Title =Prawn Cocktail});
q1.Options.Add(new Option {ID = 13,Title =Soup});
q1.SelectedAnswer = 13; // to do:get the selected answer value from DB

vm.Courses.Add(q1);

var q2 = new Course {ID = 1,Name =Mains};
q2.Options.Add(new Option {ID = 42,Title =Beef});
q2.Options.Add(new Option {ID = 43,Title =Lamp});
q2.SelectedAnswer = 16; //执行:从DB

获取所选的答案值vm.Courses.Add(q2);

return View(vm);
}

编辑器模板没有更改。

  @model课程
< div>
@ Html.HiddenFor(x => x.ID)
< h3> @ Model.Name< / h3>
@foreach(var.in Model.Options)
{
< p>
@ Html.RadioButtonFor(b => b.SelectedAnswer,a.ID)@ a.Title
< / p>
}
< / div>


In relation to this question:

StackOverflow - Grouping radio buttons in c# mvc

I have managed now to load and save the selected options from the form into a database but now the user may need to edit this.

How do I load the view with the selected user's choices? I cannot see where you can set selected Answer. On load I am getting the default values selected

解决方案

You can set the SelectedAnswer property value for each Course in your GET action and your EditorTemplate will take care of the rest.

public ActionResult Index()
{
    var vm= new OrderViewModel();

    //the below is hard coded for DEMO. you may get the data from some  
    //other place and set the course and options

    var q1 = new Course { ID = 1, Name= "Starters" };
    q1.Options.Add(new Option{ ID = 12, Title = "Prawn Cocktail " });
    q1.Options.Add(new Option{ ID = 13, Title = "Soup" });
    q1.SelectedAnswer = 13; //to do : get the selected answer value from DB

    vm.Courses.Add(q1);     

    var q2 = new Course { ID = 1, Name= "Mains" };
    q2.Options.Add(new Option{ ID = 42, Title = "Beef" });
    q2.Options.Add(new Option{ ID = 43, Title = "Lamp" });
    q2.SelectedAnswer = 16;// to do :get the selected answer value from DB

    vm.Courses.Add(q2);

   return View(vm);           
}

There is no change to be made to your editor template. It stays same as the previous answer.

@model Course
<div>
    @Html.HiddenFor(x=>x.ID)
    <h3> @Model.Name</h3>
    @foreach (var a in Model.Options)
    {
       <p>
          @Html.RadioButtonFor(b=>b.SelectedAnswer,a.ID)  @a.Title
       </p>
    }
</div>

这篇关于使用编辑器加载所选单选按钮c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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