我该怎么办模型绑定和数据显示在使用相同的model.list同一时间? [英] How can I do model binding and data display at the same time using the same model.list?

查看:112
本文介绍了我该怎么办模型绑定和数据显示在使用相同的model.list同一时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的code:

I have a code similar to this:

我的视图

@model MyCode.Models.RemarksModel

foreach (var row in Model.List)
{
    <tr>
    <td align="center"><font class="font_table_content">@row.Id</font></td>
    <td align="center"><font class="font_table_content">@row.Full_Name</font></td>
    <td align="center"><font class="font_table_content">@Html.TextBox("row.Remarks")</font></td>
}

我的模型

public class RemarksModel
{
    public IList<UserRemarks> List { get; set; }
}

我的UserRemarks对象

public class UserRemarks
{
    public virtual string Id { get; set; }
    public virtual string Full_Name { get; set; }
    public virtual string Remarks { get; set; }
}

接下来在我的控制器,我会有一些code,将记录加载到经由NHibernate的数据库一个​​IList中,然后用模型,像这里面的列表中返回的观点:

Next in my controller, I will have some code that will load the records into a IList from the DB via Nhibernate, and then return the view with the list inside the model, something like this:

[HttpGet]
public ActionResult RemarksTest()
    {
         RemarksModel model = new RemarksModel();
         model.List = LoadTheList();
         return View(model);
    }

现在,我要问的是,我怎么能接受名单后面,即得到注释值回来?

Now, what I want to ask is, how am I able to receive the list back, i.e. get the remarks value back?

[HttpPost]
public ActionResult RemarksTest(RemarksModel model)
    {
         var list = model.list;
         foreach (var remarks in list)
         {
               //do something to save the input data into code.
         }

         return View(model);
    }

实际code是比较复杂,我读过那些IDictionary的方法获得的值。然而实施这些,都会使值的以显示代替,因为code不再指model.list。

The actual code is more complex, and I've read about those IDictionary methods to receive the values. However implementing them, will cause the values not to be displayed instead, since the code no longer refers to the model.list.

任何想法如何,我可以用我上面的模型中相同的名单显示,但也接收到的数据?

Any ideas how can I display, and yet also, receive the data using the same 'list' inside my model above?

推荐答案

我想你需要改变你的看法如下:

I think you need change your view as follows:

@model IEnumerable<UserRemarks>

@using(Html.BeginForm())
{
    for(int i=0; i<@Model.Count; i++) 
    {
        <table> 
        <tr> 
            <td align="center"><font class="font_table_content">@Html.TextboxFor(m => m[i].Id)</font></td> 
            <td align="center"><font class="font_table_content">@Html.TextboxFor(m => m[i].Full_Name</font></td> 
            <td align="center"><font class="font_table_content">@Html.TextboxFor(m => m[i].Remarks")</font></td>
        </tr>
        </table>
    }

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

然后获取动作应改为:

Then the get action should be changed to:

[HttpGet] 
public ActionResult RemarksTest() 
    { 
         List<UserRermarks> model = LoadTheList();
         return View(model);
    }

和岗位操作更改为:

[HttpPost] 
public ActionResult RemarksTest(IEnumerable<UserRemarks> model) 
    { 
         foreach (var remarks in model) 
         { 
               //do something to save the input data into code. 
         } 

         return View(model); 
    } 

不幸的是我没有我的工作在计算机上的Visual Studio,所以我无法测试以上,但逻辑应该是正确的,所以如果出现问题可能只是我已经输入的东西稍微错过。

Unfortunately I don't have visual studio on the computer I am working on so I am unable to test the above but the logic should be correct so if a problem occurs it may just be that I have typed something slightly a miss.

希望这对你的作品。

这篇关于我该怎么办模型绑定和数据显示在使用相同的model.list同一时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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