问题

I have problem, that the text box value doesn't get updated with the new value in the model.

@Html.TextBoxFor(m => m.MvcGridModel.Rows[j].Id)

First the collection MvcGridModel.Rows get populated with some data, then when press button and submit the form it get new data successfully, but it doesn't update the textbox's value.

Do you have any ideas?

Thank u in advance

回答1:

That's because HTML helpers such as TextBoxFor first look in the ModelState when binding their values and only after that in the model. So if in your POST action you attempt to modify some value that was part of the initial POST request you will have to remove it from the ModelState as well if you want those changes to take effect in the view.

For example:

[HttpPost]

public ActionResult Foo(MyViewModel model)

{

// we change the value that was initially posted

model.MvcGridModel.Rows[0].Id = 56;

// we must also remove it from the ModelState if

// we want this change to be reflected in the view

ModelState.Remove("MvcGridModel.Rows[0].Id");

return View(model);

}

This behavior is intentional and it is by design. This is what allows for example to have the following POST action:

[HttpPost]

public ActionResult Foo(MyViewModel model)

{

// Notice how we are not passing any model at all to the view

return View();

}

and yet inside the view you get the values that the user initially entered in the input fields.

There's also the ModelState.Clear(); method that you could use to remove all keys from the modelstate but be careful because this also removes any associated modelstate errors, so it is recommended to remove only values from the ModelState that you intend to modify inside your POST controller action.

All this being said, in a properly designed application you should not need this. Because you should use the PRG pattern:

[HttpPost]

public ActionResult Index(MyViewModel model)

{

if (!ModelState.IsValid)

{

// there was some error => redisplay the view without any modifications

// so that the user can fix his errors

return View(model);

}

// at this stage we know that the model is valid.

// We could now pass it to the DAL layer for processing.

...

// after the processing completes successfully we redirect to the GET action

// which in turn will fetch the modifications from the DAL layer and render

// the corresponding view with the updated values.

return RedirectToAction("Index");

}

来源:https://stackoverflow.com/questions/11341545/how-to-update-the-textbox-value-html-textboxform-m-mvcgridmodel-rowsj-id

html.textboxfor id,How to update the textbox value @Html.TextBoxFor(m = m.MvcGridModel.Rows[j].Id)相关推荐

  1. 如何区分电梯卡为id卡ic卡_怎么看小区门禁卡是ic还是id

    门禁卡,在我们生活中常常都会使用到,只要轻轻一刷门就打开了.市面上常见的门禁卡分为ic卡和id卡两种,那么该如何区分它们呢?二者有哪些区别呢?我们马上来看看相关知识吧. 一.怎么看小区门禁卡是ic还是 ...

  2. win7系统,网络ID是灰色的。就是要看网络工作组计算机的那个“网络 ID”的那个按钮。

    win7系统,网络ID是灰色的.就是要看网络工作组计算机的那个"网络 ID"的那个按钮. 1首选确认自己WIN7系统不是家庭版,家庭版是没有域功能. 2其次检查服务是否自动和启用: ...

  3. html更改textbox的id,NET开发-WebForm中TextBox控件ID、Text、TextModel等属性的使用

    .NET开发-WebForm中的TextBox控件 1. TextBox概述 在ASP.NET Web应用程序中,TextBox控件是文本框控件.用于在网页表单中,让用户输入文本内容,收集用户信息,然 ...

  4. android设置列表id,学习Android绑定列表的时候提示:You must supply a resource ID for a TextView,有完整示例代码...

    这个是代码,我都是照着示例做的,但还是不行,查了一天了,到底是哪里出问题呢 万分感谢 package cn.com.ava.lesson4_fancy_view; import android.con ...

  5. 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。

    1.Windows #include <stdio.h> #include <process.h> #include <windows.h>// 线程个数 cons ...

  6. php按id获取整条数据库,Ajax取得数据库的json值,想通过id获取对应信息,但是都默认获取第一条了...

    PHP<?PHP header("Content-Type: text/html; charset=utf-8"); include('conn.php');//链接数据库 ...

  7. vue怎么根据id获取组件_vue子组件,如何根据父组件传进来的id,去查询详情,并在子组件中显示?...

    如果我在组件的created方法里面,根据id去后台查询详情,可以正常显示,不报错,但是当父组件id值改变后,并不会再次去后台查询了, ,假如我后台返回的对象时detail,如果写在computed里 ...

  8. 如何区分电梯卡为id卡ic卡_门禁卡是选择IC卡好还是ID卡好

    门禁卡目前可以封装不同的芯片,因此也可以做成ID的,也可以做成IC的,于是很多人也会这样问:"门禁卡是选择IC卡好还是ID卡好?"对于这个问题,银深源解析其中的个中原委. 其实IC ...

  9. Camera 驱动 读取摄像头ID失败问题、低温下Camera打开花屏或者读不到id、概率性读取不到id

    底层驱动读不到摄像头的ID,可以从以下几个方面做检查 首先检查硬件方面,多拿几个摄像头模组来做试验.因为会存在打样模组有问题的情况,在原理图上对清摄像头的各个脚是否都对应上了.模组是否都扣好等.这个是 ...

最新文章

  1. linux pam 解锁_linux中pam模块
  2. EASYHOOK逆向寒假生涯(20/100)
  3. mysql got signal 6_UTC - mysqld got signal 6
  4. 关于前端设置cookie
  5. java kafka 集群消费_kafka集群简单生产者消费者实例
  6. swing获取文本框内容_营销管理培训课件,大客服销售策略和技巧,全内容PPT拿来就用...
  7. 自定义Hibernate Validator规则注解
  8. python获取指定日期的前N天日期和后N天日期
  9. 通过开放重定向接管 GitHub Gist 账户,获奖$1万(GitHub $6.1万奖金系列之三)
  10. 小程序tabbar能放分包路径吗_微信小程序之如何自定义底部tabbar导航
  11. .Net中如何操作IIS(原理篇)
  12. 通过mysqladmin监控MySQL数据的服务器状态
  13. Javascript 调用MSAgent
  14. 【项目实战】Python基于决策树多分类模型实现水色图像的水质评价
  15. java 权限访问共享文件夹_在Java程序中读写windows共享文件夹
  16. python入门系列(4) -- python常见应用
  17. 京东数据化运营(三)- 复购率
  18. 使用 CSS 创建自定义鼠标光标
  19. MAC OS搭建pyhton+selenium+pycharm实现web自动化测试
  20. V4L2驱动的移植与应用(三)

热门文章

  1. 知名高校共享课程资源GitHub地址
  2. 关于python中的dict和defaultdict
  3. 难忘的一天——装操作系统(二)
  4. 关于pytorch--embedding的问题
  5. GloVe:另一种Word Embedding方法
  6. LeetCode简单题之和为零的N个唯一整数
  7. 实战清除电脑上恶意弹出广告窗口
  8. 黑客基础知识与防护(二)
  9. 用matlab怎么画视电阻率拟断面图,在MATLAB平台上实现可控源音频大地电磁反演数据三维可视化显示...
  10. Resource entery xx is already defined