【问题标题】:Getting null if i don't choose first radiobutton如果我不选择第一个单选按钮,则为 null
【发布时间】:2019-11-23 06:43:08
【问题描述】:

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

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" />
}

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

[HttpPost]
public ActionResult Index(List<Question> questions)
{
    return RedirectToAction("Index");
}

如果我选择两个单选按钮,我会在(列出问题)上获得值,但是 如果我不选择第一个单选按钮,我会得到空值,即使我选择了另一个单选按钮。

我知道我是否使用简单的 html-helper,例如
@Html.RadioButton("GetValue1", ViewBag.Answers[j].Answer1 as string) 并更改“发布”控制器

[HttpPost]
public ActionResult Index(string GetValue1)
{
    return RedirectToAction("Index");
}

我会得到值,但我想使用 StronglyTyped Helper。那么,我如何在这个项目上使用集合。我需要发送 String.empty 而不是 null。

【问题讨论】:

  • 能发一下 HTML 是如何生成的吗?
  • &lt;form action="/" method="post"&gt;&lt;label for="What_is_the_capital_of_Germany"&gt;What is the capital of Germany&lt;/label&gt; &lt;input name="[0].name" type="radio" value="London" /&gt;&lt;input name="[0].name" type="radio" value="Berlin" /&gt;&lt;label for="What_is_the_capital_of_France"&gt;What is the capital of France&lt;/label&gt; &lt;input name="[1].name" type="radio" value="Paris" /&gt;&lt;input name="[1].name" type="radio" value="Berlin" /&gt; &lt;input type="submit" value="ok" /&gt; &lt;/form&gt;

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


【解决方案1】:

这是一种可行的解决方案

查看将表单更改为以下代码

这基本上使用普通的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");
 }

【讨论】:

  • 非常感谢,FormCollection 对我很有帮助。` foreach (var key in formCollection.AllKeys) { var value = formCollection[key]; // 等等 }`
猜你喜欢
  • 2011-11-05
  • 2017-09-02
  • 1970-01-01
  • 2021-11-12
  • 2014-04-13
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多