C# 返回默认值的MVC模型属性
有些奇怪的事情正在发生,我不明白为什么。。下面是一个场景- 当我填充模型时,我有一个属性很少的模型。模型中的属性确实设置了值(通过放置断点进行检查)。它也会出现在视图中,但不会显示在文本框中。它显示默认值(通过查看页面上的item textbox进行猜测,因为它有0) 下面是我的模型-C# 返回默认值的MVC模型属性,c#,asp.net-mvc,C#,Asp.net Mvc,有些奇怪的事情正在发生,我不明白为什么。。下面是一个场景- 当我填充模型时,我有一个属性很少的模型。模型中的属性确实设置了值(通过放置断点进行检查)。它也会出现在视图中,但不会显示在文本框中。它显示默认值(通过查看页面上的item textbox进行猜测,因为它有0) 下面是我的模型- public class PriceEnquiryModel { [DisplayName("Item")] public int item { get; set; } [Display
public class PriceEnquiryModel
{
[DisplayName("Item")]
public int item { get; set; }
[DisplayName("Description")]
public string description { get; set; }
[DisplayName("UOP")]
public string uop { get; set; }
[DisplayName("UOS")]
public string uos { get; set; }
[DisplayName("Pack Description")]
public string pack_description { get; set; }
[DisplayName("Pack Size")]
public string PackSize { get; set; }
}
这是控制器;s代码-
public ActionResult Search(PriceEnquiryModel price)
{
var priceEnquiryModel = new PriceEnquiryModel();
// Read parameter values from form.
int item = Convert.ToInt32(Request.Form["txtSearch"].ToString());
int maxrow = Convert.ToInt32(Request.Form["txtmaxrow"].ToString());
string priceType = !string.IsNullOrWhiteSpace(price.priceType) && price.priceType.ToUpper().Equals("STA") ? "N" : "Y";
// Get the price information
var operationResult = priceBal.SearchPriceEnquiry(0, item, price.price_scheme, priceType, maxrow);
var priceEnquiryDomList = (List<PriceEnquiryDom>)operationResult[0].Result;
// Check if we have something
if (priceEnquiryDomList != null && priceEnquiryDomList.Count > 0)
{
// Parse the model.
priceEnquiryModel = helper.ConvertDomToModel(priceEnquiryDomList[0]);
// Prepare the list.
priceEnquiryModel.PriceEnquiryModelList = new List<PriceEnquiryModel>();
foreach (var priceEnquiryDom in priceEnquiryDomList)
{
var priceEnquiryModelListItem = helper.ConvertDomToModel(priceEnquiryDom);
priceEnquiryModel.PriceEnquiryModelList.Add(priceEnquiryModelListItem);
}
Session["mainModel"] = priceEnquiryModel;
}
// Prepare product drop down list items if searched by product desc
if (TempData.Count > 0 && TempData["Products"] != null)
{
var products = TempData["Products"] as List<ProductSearchByDescModel>;
ViewBag.Products = products;
}
return View("Index", priceEnquiryModel);
}
public ActionResult搜索(价格查询模型价格)
{
var priceinquirymodel=新的priceinquirymodel();
//从窗体中读取参数值。
int item=Convert.ToInt32(Request.Form[“txtSearch”].ToString());
int maxrow=Convert.ToInt32(Request.Form[“txtmaxrow”].ToString());
字符串priceType=!string.IsNullOrWhiteSpace(price.priceType)和&price.priceType.ToUpper().Equals(“STA”)?“N”:“Y”;
//获取价格信息
var operationResult=priceBal.SearchPriceInquiry(0,项目,price.price\u方案,priceType,maxrow);
var priceInquiryDomList=(列表)操作结果[0]。结果;
//看看有没有什么
if(priceinquirydomlist!=null&&priceinquirydomlist.Count>0)
{
//解析模型。
PriceInquiryModel=helper.ConvertDomToModel(PriceInquiryDomList[0]);
//准备清单。
PriceInquiryModel.PriceInquiryModelList=新列表();
foreach(priceinquirydom列表中的var priceinquirydom)
{
var priceinquiryModelListItem=helper.convertdomotodel(priceinquirydom);
PriceInquiryModel.PriceInquiryModelList.Add(PriceInquiryModelListItem);
}
会话[“mainModel”]=价格查询模型;
}
//如果按产品描述搜索,则准备产品下拉列表项
如果(TempData.Count>0&&TempData[“产品”!=null)
{
var products=TempData[“产品”]作为列表;
ViewBag.Products=产品;
}
返回视图(“索引”,PriceInquiryModel);
}
这是视图上的模型(调试时)
public class PriceEnquiryModel
{
[DisplayName("Item")]
public int item { get; set; }
[DisplayName("Description")]
public string description { get; set; }
[DisplayName("UOP")]
public string uop { get; set; }
[DisplayName("UOS")]
public string uos { get; set; }
[DisplayName("Pack Description")]
public string pack_description { get; set; }
[DisplayName("Pack Size")]
public string PackSize { get; set; }
}
这是我在视图上渲染模型的方式-
public class PriceEnquiryModel
{
[DisplayName("Item")]
public int item { get; set; }
[DisplayName("Description")]
public string description { get; set; }
[DisplayName("UOP")]
public string uop { get; set; }
[DisplayName("UOS")]
public string uos { get; set; }
[DisplayName("Pack Description")]
public string pack_description { get; set; }
[DisplayName("Pack Size")]
public string PackSize { get; set; }
}
这是运行后的页面-
public class PriceEnquiryModel
{
[DisplayName("Item")]
public int item { get; set; }
[DisplayName("Description")]
public string description { get; set; }
[DisplayName("UOP")]
public string uop { get; set; }
[DisplayName("UOS")]
public string uos { get; set; }
[DisplayName("Pack Description")]
public string pack_description { get; set; }
[DisplayName("Pack Size")]
public string PackSize { get; set; }
}
有人知道发生了什么事吗?我已经在多个页面上做了同样的事情,并且都按预期运行
提前谢谢。
Rohit问题在于,您的方法具有参数
PriceInquiryModel price
,但随后返回了PriceInquiryModel
的新实例(名为PriceInquiryModel
)。模型绑定的过程包括绑定模型并将其值添加到ModelState
(以及任何验证错误)
返回视图时,html帮助程序方法使用来自ModelState
(而不是模型值)的值,因此帮助程序将忽略尝试更改值(我假设是priceinquirymodel=helper.convertdomodel(priceinquirydomlist[0]);
正在执行的操作)
有关为什么这是默认行为的解释,请参阅
在为
priceinquirymodel
的属性设置新值之前,可以调用ModelState.Clear()
一个选项,您能告诉我们该操作吗?当您将模型操作传递给视图时,可能会出现一些问题。是否有JavaScript将这些值弄乱了?@beautifulcoder,不,在渲染时视图上没有这样的js..@MahediSabuj-在模型中有值。。它只是没有显示在页面上。请看第二幅图,它确实有值。您需要显示控制器方法。GET方法是否在签名中有参数priceinquirymodel
?通过在视图中添加@Model.description
来执行测试,以检查其正确性-如果正确,则为ModelState
问题。