本文介绍了模型绑定HTML:ASP不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个动态的textarea 是在 DOM 动态使用<$ C $下加入的组> JS 。
那些的textarea 的,我希望他们在控制器,确实如此。
我收到 model.actionLis T与code:

I made a dynamic textarea's group that are added in DOM dynamically by using JS.Those textarea's, I want them in the controller, indeed.I receive null values in the model.actionList with that code:

下面是 HTML 生成的:

<div class="input_fields_wrap">
<div><textarea class="form-control text-box multi-line" name="model.actionList[1].probleme"></textarea> <textarea class="form-control text-box multi-line" name="model.actionList[1].action"></textarea></div>

<div><textarea class="form-control text-box multi-line" name="model.actionList[2].probleme"></textarea> <textarea class="form-control text-box multi-line" name="model.actionList[2].action"></textarea></div>

<div><textarea class="form-control text-box multi-line" name="model.actionList[3].probleme"></textarea><textarea class="form-control text-box multi-line" name="model.actionList[3].action"></textarea></div>

</div>

通过这种 JS

$(wrapper).append('<div>Probleme: <textarea class="form-control text-box multi-line" name="model.actionList[' + x + '].probleme"></textarea> Action: <textarea class="form-control text-box multi-line" name="model.actionList[' + x + '].action"></textarea>@*<a href="#" class="remove_field">Remove</a>*@</div>'); //add input box

服务器端,这是控制器

public ActionResult Create(AuditViewModel model)
{...}

这就是著名的视图模型,这里是有趣的部分:

This is the famous Viewmodel part that are interesting here:

public class AuditViewModel
    {public List<Actions> actionList { get; set; }}

最后,这是动作类:

public class Actions
{
    public string probleme { get; set; }
    public string action { get; set; }
}

什么是错的?

在此先感谢!

推荐答案

所有索引首先从开始的 0 其次使用它像的ActionList [指数] .SomePeoperty 作为的ActionList是你的模式的属性:

First of all Indexing starts from 0 and secondly use it like actionList[index].SomePeoperty as actionList is property of your Model:

当你写的:

@model SomeModel

@Html.TextAreaFor(x=>x.SomeProperty)

它呈现这样的:

<textarea name="SomeProperty" id="SomeProperty"></textarea>

它并不像渲染:

<textarea name="model.SomeProperty" id="model.SomeProperty"></textarea>

因此​​,尝试它象下面这样:

So try it like below:

<div>
<textarea class="form-control text-box multi-line" name="actionList[0].probleme"></textarea>
<textarea class="form-control text-box multi-line" name="actionList[0].action">
</textarea>
</div>

<div>
<textarea class="form-control text-box multi-line" name="actionList[1].probleme"></textarea>
<textarea class="form-control text-box multi-line" name="actionList[1]action">
</textarea>
</div>

<div>
<textarea class="form-control text-box multi-line" name="actionList[2].probleme"></textarea>
<textarea class="form-control text-box multi-line" name="actionList[2].action">
</textarea>
</div>

这篇关于模型绑定HTML:ASP不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:39