MVC的Razor视图:如何呈现的文本框列表℃的列表,任务>在模型? [英] MVC Razor View: How to render a list of text boxes for a List<Task> in the model?

查看:141
本文介绍了MVC的Razor视图:如何呈现的文本框列表℃的列表,任务>在模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表与LT;任务过夜。此列表中包含2个任务(比如说)在其

I have a List<Task> in my model. This list contains 2 tasks(say) in it

public List<Task> Tasks { get; set; }

public class Task    {
    public Task()
    {
        Title="";
        Total= 0;
    }

    public string Title{ get; set; }

    public int Total{ get; set; }
}

现在在我的剃须刀的看法,我想呈现两个文本框为每个在列表&LT的任务的;任务&GT; 我的模型的。

Now in my razor view, I want render 2 text boxes for each of the Tasks in the List<Tasks> of my model.

我没有循环,刚放置直接的文本框,如:

I didn't loop, just placed direct text boxes like:

@Html.TextBoxFor(m => m.Tasks[0].Title, new { @maxlength = "50"})
@Html.TextBoxFor(m => m.Tasks[0].Total, new { @maxlength = "2"})
<br>
@Html.TextBoxFor(m => m.Tasks[1].Title, new { @maxlength = "50"})
@Html.TextBoxFor(m => m.Tasks[1].Total, new { @maxlength = "2"})

这使得形式很好,但点击提交按钮在FF不会做任何事情。
然而,它张贴罚款IE9。

This renders the form fine, but clicking the submit button doesn't do anything in FF. However it posts fine in IE9.

查看源这样表示生成的这个网站:

View source shows this html generated like this:

<input id="Tasks_0__Title" maxlength="50" name="Tasks[0].Title" type="text" value="" />
<input data-val="true" data-val-number="The field Total must be a number." data-val-required="The Total field is required." id="Tasks_0__Total" maxlength="2" name="Tasks[0].Total" type="text" value="" /> 

这个HTML看起来不正确的。它有 NAME =任务[0]。总这似乎很奇怪。

This HTML doesn't look right. It has name="Tasks[0].Total" which seems odd.

我应该怎么做,这样我可以从列表&LT访问文本框的值;&GT; 在POST之后我的控制器

How should I do this so that I can access the text box values from List<> in my controller after post?

感谢

编辑:
我只是不停一行测试。这是我在FF看到HTML

I just kept one row for test. This is the html I see in FF.

<input id="Tasks_0__Title" type="text" value="" name="Tasks[0].Title" maxlength="50">
<input id="Tasks_0__Total" type="text" value="" name="Tasks[0].Total" maxlength="2" data-val-required="The Total field is required." data-val-number="The field Total must be a number." data-val="true">

当我点击提交按钮这不会发布。

This doesn't post when I click the submit button.

现在,如果我换 NAME =任务[0] .title伪来命名=Tasks_0__Title和NAME =Tasks_0__Total
 在Firebug它张贴罚款。

Now if I change name="Tasks[0].Title" to name="Tasks_0__Title" and name="Tasks_0__Total" in FIREBUG it posts fine.

如果我删除的名字完全也张贴在FF罚款

If I delete name completely it also posts fine in FF

推荐答案

您应该使用任务[0]。总任务[1] 。总而不是项目

@Html.TextBoxFor(m => m.Tasks[0].Title, new { @maxlength = "50"})
@Html.TextBoxFor(m => m.Tasks[0].Total, new { @maxlength = "2"})
<br/>
@Html.TextBoxFor(m => m.Tasks[1].Title, new { @maxlength = "50"})
@Html.TextBoxFor(m => m.Tasks[1].Total, new { @maxlength = "2"})

NAME =任务[0]。总不奇怪。这究竟是怎么输入应为使模型绑定来获取值回了POST操作来命名。见<一href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow>这个博客帖子对铁丝格式列表和字典打交道时所使用的默认模型粘合剂。

name="Tasks[0].Total" is not odd. That's exactly how the input should be named in order for the model binder to fetch the value back in the POST action. See this blog post for the wire format used by the default model binder when dealing with lists and dictionaries.

这是说我会使用编辑器模板建议您=>,而不是写那些5行code在你看来与替换它们:

This being said I would recommend you using editor templates => instead of writing those 5 lines of code in your view replace them with:

@Html.EditorFor(x => x.Tasks)

和则相应的编辑模板内(〜/查看/共享/ EditorTemplates / Task.cshtml ),这将在<$ C $自动呈现的每个元素C>任务系列:

and then inside the corresponding editor template (~/View/Shared/EditorTemplates/Task.cshtml) which will be automatically rendered for each element in the Tasks collection:

@model Task
@Html.TextBoxFor(m => m.Title, new { @maxlength = "50"})
@Html.TextBoxFor(m => m.Total, new { @maxlength = "2"})    
<br/>

现在你可以离开编辑模板担心适当的命名约定,等...

Now you can leave the editor templates worry about proper naming convention, etc...

至于您的POST操作而言:

As far as your POST action is concerned:

[HttpPost]
public ActionResult Foo(MyViewModel model)
{
    // model.Tasks should be correctly bound here
    ...
}

这篇关于MVC的Razor视图:如何呈现的文本框列表℃的列表,任务&GT;在模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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