如果我不选择第一个单选按钮,则为null

时间:2019-07-14 05:42:41

标签: c# asp.net-mvc radio-button

我在项目Asp.net MVC5上有一些代码: 型号

make MINGW=x86_64-w64-mingw32-gcc BINUTILS=~/binutils-2.32

控制器:

public class Question
{
    public int? ID { get; set; }
    public string name { get; set; }
}
public class Answer
{
    public int? ID { get; set; }
    public string Answer1 { get; set; }
    public string Answer2 { get; set; }
    public int? QuestionID { get; set; }
}

并查看索引:

public class HomeController : Controller
{

    public ActionResult Index()
    {
        List<Question> questions = new List<Question> {
            new Question { ID = 1, name = "What is the capital of Germany" },
            new Question {ID =2 , name ="What is the capital of France" } 
        };
        List<Answer> answers = new List<Answer>
        {
            new Answer {Answer1="London", Answer2 = "Berlin", QuestionID= 1},
            new Answer {Answer1="Paris", Answer2 = "Berlin", QuestionID = 2}
        };
        ViewBag.Answers = answers;
        return View(questions);
    }
}

此表格后,我在这里得到结果:

@using WebApplication2.Models;
@model List<Question>
@{
    ViewBag.Title = "Home Page";
}

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        @Html.Label(Model[i].name)
        {
            for (int j = 0; j < ViewBag.Answers.Count; j++)
            {
                if (Model[i].ID == ViewBag.Answers[j].QuestionID)
                {
                    @Html.RadioButtonFor(a => Model[i].name, ViewBag.Answers[j].Answer1 as string)
                    @Html.RadioButtonFor(a => Model[i].name, ViewBag.Answers[j].Answer2 as string)
                }
            }
        }

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

如果我同时选择两个单选按钮,则会获得(列出问题)的值,但是 如果我没有选择第一个单选按钮,我将得到null,即使我选择了另一个单选按钮。

我知道我是否使用像
这样的简单html-helper [HttpPost] public ActionResult Index(List<Question> questions) { return RedirectToAction("Index"); } 并更改“发布”控制器

@Html.RadioButton("GetValue1", ViewBag.Answers[j].Answer1 as string)

我将获得值,但是我想使用StronglyTyped Helper。因此,如何在该项目上使用集合。我需要发送String.empty而不是null。

1 个答案:

答案 0 :(得分:0)

这是一种可行的解决方案

查看,将表单更改为下面的代码

这基本上使用普通的html标签进行绑定

@using WebApplication2.Models;
@model List<Question>
@{
    ViewBag.Title = "Home Page";
    var answers = (List<Answer>)ViewBag.Answers;
}

@using (Html.BeginForm())
{
    <br />
    foreach (var question in Model)
    {
        @Html.Label(question.name)
        {
            foreach (var answer in answers)
            {
                if (question.ID == answer.QuestionID)
                {                
                    <input type="radio" name="@question.controlName" value="@answer.Answer1"/>@answer.Answer1 
                    <input type="radio" name="@question.controlName" value="@answer.Answer1"/>@answer.Answer2
                }
            }
        }

        <br />

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

模型我修改了模型,为每个广播组赋予唯一的名称

public class Question
{
    public int? ID { get; set; }
    public string name { get; set; }
    public string controlName { get; set; } //this will be the name in the input field
}

控制器在这里,我真的不知道您是如何生成列表的,但我希望它来自数据库。

public class HomeController : Controller
{
    //here I added the controlName variable to the Question, this is better to identify each question by name
    [HttpGet]
    public ActionResult Index()
    {
        List<Question> questions = new List<Question> {
            new Question { ID = 1, name = "What is the capital of Germany", controlName = "germany"},
            new Question {ID =2 , name ="What is the capital of France", controlName = "france"}
        };
        List<Answer> answers = new List<Answer>
        {
            new Answer {Answer1="London", Answer2 = "Berlin", QuestionID= 1},
            new Answer {Answer1="Paris", Answer2 = "Berlin", QuestionID = 2}
        };
        ViewBag.Answers = answers;
        return View(questions);
    }
}

//Here am using FormCollection to receive the inputs
[HttpPost]
public ActionResult Index(FormCollection questions)
{
    List<string> controlNames = new List<string>{"germany","france"}; //this is supposed to come from the db if db is in use
    //next create a dictionary of string and string for the controlName and the answer selected for it
    Dictionary<string, string> userAnswer = new Dictionary<string, string>();
    foreach (var controlName in controlNames)
    {
        userAnswer.Add(controlName, questions[controlName]);
    }

    //Manipulate the dictionary here and save to the db
    return RedirectToAction("Index");
 }