本文介绍了验证清除所有显示字段..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



当发生验证错误时,所有displayFor值都被清除..如何解决这个问题..



我尝试过:



查看:@ Html.DisplayFor(t => t.RateCode,new { id =RateCode,@ class =form-control})

@ Html.TextBox(ItemName,null,new {id =ItemName,@ class =form-control ,autocomplete =off})还有一个像这样的字段



控制器:

[HttpPost]

public ActionResult Create(ItemRates itemrates)

{

if(ModelState.IsValid)

{

itemrates.GetDate = DateTime.Now;

itemrates.DelFlag =N;

itemrates.GetUser = Session [LogedUser]。ToString();

itemrates.CompCode =CMP-001;

db.ItemRates.Add(itemrates);

db.SaveChanges();

ViewBag.Message =记录保存成功;

ModelState.Clear();

var maxId =( db.ItemRates.Select(x => (int?)x.Id).Max()?? 0)+ 1;

var RC =IRC-+ maxId;

return View(new ItemRates {RateCode = RC});

}

else {ViewBag.ErrMsg =输入必填字段; }

返回查看(itemrates);

}



查看:@ Html.DisplayFor(t => ; t.ItemCode,new {id =ItemCode,@ class =form-control})



自动填充项目名称:



if(data){

$('#ItemCode')。val(data.icode);

}

Dear Friends,

When validation error occurs all displayFor Values are cleared.. how to resolve this..

What I have tried:

View: @Html.DisplayFor(t => t.RateCode, new { id = "RateCode", @class = "form-control" })
@Html.TextBox("ItemName",null, new { id = "ItemName", @class = "form-control", autocomplete = "off" }) "one More Fields like this"

Controller:
[HttpPost]
public ActionResult Create(ItemRates itemrates)
{
if (ModelState.IsValid)
{
itemrates.GetDate = DateTime.Now;
itemrates.DelFlag = "N";
itemrates.GetUser = Session["LogedUser"].ToString();
itemrates.CompCode = "CMP-001";
db.ItemRates.Add(itemrates);
db.SaveChanges();
ViewBag.Message = "Record Saved Successfully";
ModelState.Clear();
var maxId = (db.ItemRates.Select(x => (int?)x.Id).Max() ?? 0) + 1;
var RC = "IRC-" + maxId;
return View(new ItemRates { RateCode = RC });
}
else { ViewBag.ErrMsg = "Enter Mandatory Fields"; }
return View(itemrates);
}

View: @Html.DisplayFor(t => t.ItemCode, new { id = "ItemCode", @class = "form-control" })

Autocomplete item name:

if (data) {
$('#ItemCode').val(data.icode);
}

推荐答案


这篇关于验证清除所有显示字段..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 21:09