从表单传递具有可变数量的输入的数据-MVC 5 [英] Passing data from Form with variable number of inputs - MVC 5
问题描述
我一直在这里寻找类似的问题,但仍然找不到解决我问题的方法.
I've been looking for similar questions in here but still couldn't find a solution to my problem.
我有一个包含一些文本和表单的页面,它们都共享相同的ViewModel,如下所示:
I have a page that contains some text and a form and they both share the same ViewModel as follows:
public class MyViewModel
{
public IEnumerable<WordingB> WordingBs { get; set; }
public IEnumerable<WordingC> WordingCs { get; set; }
public IEnumerable<Question> Questions { get; set; }
}
以下是有关WordingB,WordingC和Question的更多信息:
Here's a bit more detail about WordingB, WordingC and Question:
public class WordingB
{
public string EOW { get; set; }
}
public class WordingC
{
public string EOW { get; set; }
}
public class Question
{
public string QuestionText { get; set; }
public string Answer {get; set;}
}
这是相关页面:
@model MyProject.ViewModels.MyViewModel
<div class="col-md-6 masonry listview-block">
@foreach (var wording in Model.WordingBs)
{
<div class="block">
<p>@Html.Raw(@wording.EOW)</p>
</div>
}
@foreach (var wording in Model.WordingCs)
{
<div class="block">
<p>@Html.Raw(@wording.EOW)</p>
</div>
}
</div>
@using (Ajax.BeginForm("Routing", "Partials", new AjaxOptions { UpdateTargetId = "Target", LoadingElementId = "spinner", HttpMethod = "POST", InsertionMode = InsertionMode.Replace }))
{
<div id="quick-post" class="block-body form-validation">
@foreach (var question in Model.Questions)
{
<div class="form-group">
<label for="QuestionText">@question.QuestionText</label>
<input type="text" class="form-control input-sm input-sm" name="Answer">
</div>
}
<div class="form-group">
<label for="postcode">PostCode</label>
<input type="text" class="form-control input-sm validate[required] input-sm" name="postcode" value=@Request.QueryString["postcode"]>
</div>
<div class="form-group">
<label>Loss Description</label>
<textarea></textarea>
</div>
<input type="submit" class="btn btn-primary btn-xs" value="Route">
</div>
</div>
</div>
}
想法是某些管理员可以向表单添加问题.(问题存储在一个表中)有一个控制器使用MyViewModel并向视图返回我需要的模型.
The idea is that some Admin person is able to add questions to the form. (questions are stored in a table) There's a controller that uses the MyViewModel and returns the model that I need to the view.
public ActionResult EOW()
{
QuestionsandWording viewModel = new QuestionsandWording();
viewModel.Questions = // first query
viewModel.WordingBs = // second query
viewModel.WordingCs = // third query
return View(viewModel);
}
我现在面临的问题是将数据从表单传递到控制器.就我而言,该表格可以包含0到30或40个问题!我觉得自己已经达到了学问的极限,我急需咨询.
The problem that I am facing now is passing the data from my form to a controller. The form can have zero to 30 or 40 questions as far as I'm concerned! I feel like I've hit the limit of my knowledge and I'm in serious need of advice.
推荐答案
在cosset和Derek的帮助下,我设法找到一种解决方法,如下所示:
With the help of cosset and Derek, I managed to find a workaround as follows:
1)我为输入元素起了这样的名字:
1) I gave a name to my input element like this:
<input type="text" name="Answer">
我不需要按照答案之一的建议重复使用Answer [i],因为框架会自动将所有具有相同名称的输入绑定到一个List元素中,该元素可以用作我的方法的参数.像这样:
I didn't need to iterate with Answer[i] as suggested on one of the answers as the framework automatically binds all inputs with same name into a List element that my method can take as an argument. Like this:
public ActionResult Routing(List<string> Answer){}
2)我还需要在我的路由"方法中使用我的标签标签的值,并且不知道如何执行此操作.再次,Cosset建议我使用一个隐藏字段,并为其指定标签文本的值.这工作得很好.
2) I also needed to use the value of my label tags in my Routing method and had no clue how to do it. Again, Cosset suggested that I use a hidden field and give it a value of the label text. This worked perfectly.
<label for="Answer">@question.QuestionText</label>
<input type="hidden" name="QuestionText" value=@question.QuestionText />
方法现在看起来像这样:
And the method now looks like this :
public ActionResult Routing(List<string> Answer, List<string> QuestionText){}
现在我已经有了所需的东西.将所有数据从表单传递到控制器(标签和输入),而不必担心MyViewModel.
Now I've got what I needed. To pass all data from my form to the controller (Both labels and inputs) without having to worry about the MyViewModel.
为了学习,我很想知道是否还有其他方法可以实现这一目标.
For the sake of learning, I would quite like to know if there are any other ways of achieving this at all.
这篇关于从表单传递具有可变数量的输入的数据-MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!