结合难度在列表回传 [英] Binding difficulty upon postback of list

查看:105
本文介绍了结合难度在列表回传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有回发新数据的难度被输入。看来,尽管对数据所做提交之前更改发送到视图中的数据被发送回控制器。

I am having the difficulty to post back the new data being entered. It seems that the data sent to the view are sent back to the controller, despite changes made to the data before submit.

我的code是如下:

控制器
     公共类GroupRateController:控制器
    {
        //
        // GET:/ GroupRate /

Controller public class GroupRateController : Controller { // // GET: /GroupRate/

    public ActionResult Index()
    {
        GroupRateModel model = new GroupRateModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(GroupRateModel model)
    {
        model.Save(model);
        return View(model);
    }

}

查看

 @model MvcApplication1.Models.GroupRateModel

@{
    View.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>
@using (Html.BeginForm())
{

@Html.ValidationSummary()

<table>
<thead>

</thead>
<tr><th>Rate Group</th><th>Default Amount</th><th>Client Amount</th></tr>
@foreach (var item in @Model.ClientRateDetails)
{
<tr><td>@item.RateGroupName</td><td align="right">@Html.DisplayFor(m => @item.RateGroupID)</td><td>@Html.EditorFor(model => item.ClientRate)</td></tr>

}
</table>

<p> <input type ="submit"  value="Save" id="submit" /></p>
}

示范

    using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Models
{
    public class GroupRateModel 
    {
        public List<ClientRateDetailsModel> ClientRateDetails = new List<ClientRateDetailsModel>() ;
        public string Name { get; set; }


        public GroupRateModel()
        {
                    ClientRateDetails.Add(new ClientRateDetailsModel
                    {
                        RateGroupID = 1,
                        RateGroupName = "Test1",
                        ClientRate = 100
                    });

                    ClientRateDetails.Add(new ClientRateDetailsModel
                    {
                        RateGroupID = 2,
                        RateGroupName = "Test2",
                        ClientRate = 200
                    });

                    ClientRateDetails.Add(new ClientRateDetailsModel
                    {
                        RateGroupID = 3,
                        RateGroupName = "Test3",
                        ClientRate = 300
                    });

        }

        public void Save(GroupRateModel model)
        {
             foreach (var item in model.ClientRateDetails)
                {
                    //...;
                }
            }
        }


    public class ClientRateDetailsModel
    {       
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:00.00}", NullDisplayText = "")]
        [Range(0, (double)decimal.MaxValue, ErrorMessage = "Please enter a valid rate")]
        public decimal? ClientRate { get; set; }
        public int? RateGroupID { get; set; }
        public string RateGroupName { get; set; }
    }

}

推荐答案

这可能是因为你的输入控件的名称没有正确名称为模型粘结剂能够正确读取值。我也看到 ClientRateDetails 不是财产,但模型中的字段,将无法正确约束。因此,这里就是我会建议你提高code:

This might be because the names of your input controls don't have correct names for the model binder to be able to fetch the values correctly. Also I see that the ClientRateDetails is not a property but a field in your model which won't be bound correctly. So here's how I would suggest you to improve your code:

开始与模型:

public class GroupRateModel 
{
    public IEnumerable<ClientRateDetailsModel> ClientRateDetails { get; set; }
    public string Name { get; set; }

    public GroupRateModel()
    {
        // Remark: You cannot assign your ClientRateDetails collection here
        // because the constructor will be called by the default model binder
        // in the POST action and it will erase all values that the user
        // might have entered
    }

    public void Save(GroupRateModel model)
    {
        foreach (var item in model.ClientRateDetails)
        {
            //...;
        }
    }
}

public class ClientRateDetailsModel
{       
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:00.00}", NullDisplayText = "")]
    [Range(0, (double)decimal.MaxValue, ErrorMessage = "Please enter a valid rate")]
    public decimal? ClientRate { get; set; }
    public int? RateGroupID { get; set; }
    public string RateGroupName { get; set; }
}

然后控制器:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = new GroupRateModel();
        model.ClientRateDetails = new[]
        {
            new ClientRateDetailsModel
            {
                RateGroupID = 1,
                RateGroupName = "Test1",
                ClientRate = 100
            },
            new ClientRateDetailsModel
            {
                RateGroupID = 2,
                RateGroupName = "Test2",
                ClientRate = 200
            },
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(GroupRateModel model)
    {
        model.Save(model);
        return View(model);    
    }
}

,然后相应的视图:

and then the corresponding view:

@model MvcApplication1.Models.GroupRateModel
@{
    View.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <table>
    <thead>
        <tr>
            <th>Rate Group</th>
            <th>Default Amount</th>
            <th>Client Amount</th>
        </tr>
    </thead>
    @Html.EditorFor(x => x.ClientRateDetails)
    </table>
    <p><input type ="submit"  value="Save" id="submit" /></p>
}

再有相应的编辑模板(〜/查看/主页/ EditorTemplates / ClientRateDetailsModel.cshtml

@model MvcApplication1.Models.ClientRateDetailsModel
<tr>
    <!-- Make sure you include the ID as hidden field 
         if you want to get it back inside the POST action
    -->
    @Html.HiddenFor(x => x.RateGroupID)
    <td>@Model.RateGroupName</td>
    <td align="right">@Model.RateGroupID</td>
    <td>@Html.EditorFor(x => x.ClientRate)</td>
</tr>

这篇关于结合难度在列表回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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