列表回发时绑定困难

2024-02-08

我很难回发输入的新数据。尽管在提交之前对数据进行了更改,但发送到视图的数据似乎仍被发送回控制器。

我的代码如下:

控制器 公共类 GroupRateController :控制器 { // // 获取:/GroupRate/

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

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

}

View

 @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>
}

Model

    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不是属性,而是模型中无法正确绑定的字段。因此,我建议您改进代码的方法如下:

从模型开始:

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);    
    }
}

然后是相应的视图:

@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>
}

然后有一个相应的编辑器模板(~/Views/Home/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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

列表回发时绑定困难 的相关文章

随机推荐

  • React/redux,显示多个组件,共享相同的操作,但具有不同的状态

    假设我有一个可重复使用的容器 它是一个具有多个页面的向导 向导状态由 redux actions 驱动 当一个动作被触发时 我使用一个减速器来更新我的状态 如果我想要复制多个向导并拥有自己的状态怎么办 我认为必须有一种方法可以让某个动态减速
  • 在nodejs(express)中的router.route()中设置中间件

    我想要它做什么 router post xxxx authorize xxxx function authorize req res next if xxx res send 500 else next 我想检查每条路线的会话 但既然路由器
  • 如何在没有 if 语句的情况下做出决定

    我正在学习 Java 课程 但我们还没有正式学习 if 语句 我在学习的时候看到这个问题 编写一个名为 pay 的方法 它接受两个参数 一个代表助教工资的实数 一个代表助教本周工作时数的整数 该方法应该返回付给TA多少钱 例如 调用 pay
  • @NSManaged 是做什么的?

    我在不同的场合都遇到过这个关键词 我有点知道它应该做什么 但我真的想更好地理解它 我注意到了什么 NSManaged 不是基于文档 而是通过重复使用 它神奇地取代了键值编码 大致相当于 dynamic在 Objective C 中 我不太了
  • 如何在 Haskell 中安装旧版本的 base

    我已经安装了Haskell平台 并且有7 10 3版本的ghci 其中有4 8 2 0版本的base 我需要安装gloss 1 8 哪个需要base 4 7 基础版本 我的问题是 当我已经有了新版本时 如何安装这个旧版本 是否可以 或者我必
  • ms-access:通过打印来填写申请表

    我将打印访问报告 该报告不会印刷成普通的白皮书 它将打印在带有复选框和字段的纸张上 我需要根据访问数据打印这些复选框和字段 有没有任何库可以让这变得更容易 是否有一个功能可以帮助在特定坐标上打印 请注意 我需要在数千份表格上打印 并且我必须
  • 使用准备好的语句后 SELECT LAST_INSERT_ID() 返回 0

    我正在使用 MySQL 和准备好的语句来插入BLOB记录 jpeg 图像 执行准备好的语句后 我发出一个SELECT LAST INSERT ID 它返回 0 在我的代码中 我在执行命令后放置了一个断点 并在 MySQL 命令 监视器 窗口
  • 为什么 Chrome 开发工具显示 200 状态代码而不是 304

    当我用 Chrome 测试缓存处理中的奇怪行为时 我问了一些关于它的问题 here https stackoverflow com questions 67016037 chrome doesnt send if none match he
  • Discord.js V12 粗鲁言语过滤器不起作用

    所以我添加了一个粗鲁的单词过滤器 每当有人说这个单词 小写或大写 时 它就会删除他们的消息并回复一些内容 然后回复会在几秒钟内被删除 这是我当前的代码 但它不读取rudeWords当我在聊天中写下任何粗鲁的话时 它不会做任何事情 clien
  • Rails4:康康舞还是康康康舞?使用 has_secure_password

    我正在尝试实现某种类型的用户 以便用户可以编辑数据 而其他用户只能读取 user rb class User lt ActiveRecord Base has secure password validates presence of em
  • AVAudioRecorder 内存泄漏

    我希望有人能在这件事上支持我 我一直在开发一个应用程序 该应用程序允许最终用户录制一个小音频文件以供以后播放 并且正在测试内存泄漏 当 AVAudioRecorder 的 停止 方法尝试关闭其正在录制的音频文件时 我仍然经常遇到内存泄漏 这
  • create-react-app 返回错误:执行时找不到模块“react-scripts/scripts/init.js”

    当我尝试使用 npm 和yarn 创建一个 React 项目时 它显示以下错误 我尝试重新安装节点并确保它是最新的 以及通过运行 npm install g create react app latest 来创建 react app 我还删
  • glFlush() vs [[self openGLContext]lushBuffer] vs glFinish vs glSwapAPPLE vs aglSwapBuffers

    使用 NSOpenGLView 时有几个类似的 OpenGL 操作 glFlush self openGLContext flushBuffer glFinish glSwap苹果 egl交换缓冲区 何时应该使用其中的每一个 在示例应用程序
  • 有没有办法可以检测图像方向并将图像旋转到直角?

    我正在制作一个修复扫描文档的脚本 现在我需要一种方法来检测图像方向并旋转图像 以便其旋转正确 现在我的脚本不可靠而且不够精确 现在我寻找一条线 它会旋转它正确看到的第一条线 但这几乎不起作用 除了一些图像 img before cv2 im
  • intellij idea - 错误:java:无效源版本 1.9

    我正在尝试运行我的 JSQL 解析器类 但是我得到了Error java invalid source release 1 9 我尝试跟随这个答案 https stackoverflow com a 42650624 7327018 我更改
  • Spring MVC 不记录所有异常

    我将 Spring MVC 设置为使用公共日志记录来记录异常 但发现某些运行时异常没有被记录 这是 spring 提供的默认异常解析器的 bean 配置
  • Firebase 多语言密码重置电子邮件

    大家好 Firebase 及其所有出色功能 提供了一项服务 使经过身份验证的用户可以重置其密码 但这项服务只考虑一种文本 仪表板上定义的文本 是否可以用多种语言获得此内容 我需要这个功能 Firebase 朋友 因为我想你会看到这个问题 你
  • 无法让elasticsearch服务在ubuntu 17中运行?

    我按照此处的步骤安装并让 elasticsearch 工作 https www digitalocean com community tutorials how to install and configure elasticsearch
  • 使用 Rapids.ai 版本 0.11+ 将 cuDF 和 cuML 安装到 Colab 中

    我正在尝试将带有 cuDF 和 cuML 的 Rapids 库安装到 Colab 会话中 并根据此示例执行代码 从在 Google Colab 笔记本上安装 RAPIDS 库 https stackoverflow com question
  • 列表回发时绑定困难

    我很难回发输入的新数据 尽管在提交之前对数据进行了更改 但发送到视图的数据似乎仍被发送回控制器 我的代码如下 控制器 公共类 GroupRateController 控制器 获取 GroupRate public ActionResult