无法在UpdateModel之后获取值 [英] Unable to get values after UpdateModel

查看:76
本文介绍了无法在UpdateModel之后获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个模型类付款,其中有(在付款构造函数中)我传递其属性值的值。它在HttpGet Action中运行良好。但是当我发布此表格时,付款物业没有这些物业。





付款paymentobj1 =新付款(); //这里Paymentobj1   全部  < span class =code-keyword> 此属性
UpdateModel(paymentobj1); //执行UpdateModel后,paymentobj1 将会丢失。





请建议我这有什么问题。



  public  class Payment 
{
public Payment()
{
//获取值静态方法 设置 进入财产。
ProductItems = StaticClass.StaticMethod();
}
public list< items> ProductItems {get; set ;}
}


//这里 地点其中 获取属性:控制器
[HttpPost]
public ActionResult 索引(付款paymentobj)
{
//这里我收到payment.ProductItems值 null ???

付款paymentobj1 =新付款(); //这里Paymentobj1 全部 < span class =code-keyword> 此属性
UpdateModel(paymentobj1); //执行UpdateModel后,paymentobj1 将丢失。

}

解决方案

当您发布到表单时,对象将丢失,您所拥有的只是一个集合从Web表单传入的键值对。您需要做的是创建一个新对象并尝试将表单字段映射到您的对象。如果您正确创建了Web表单,TryUpdateModel方法将为您执行此操作。



您的代码应如下所示:



 [HttpPost] 
public ActionResult Index(FormCollection formData)
{
付款paymentobj1 = 付款();

// 仅允许来自表单的数据
if (TryUpdateModel(paymentobj1,formData))
{

}


}


Hi Everybody,

I have a Model Class Payment in which have (In Payment Constructor) i am passing the values of its properties values. It is working fine in the HttpGet Action. but when i am posting this form then Payment Properties does not having the properties.


Payment paymentobj1 = new Payment();  //Here Paymentobj1 having all values in this properties
UpdateModel(paymentobj1);  //After executing UpdateModel the paymentobj1 values will be loss.



Please suggest me what is wrong with that.

public class Payment
{
public Payment()
{
//Get the Value from the Static Method and set into the property.
ProductItems = StaticClass.StaticMethod();
}
public list<items> ProductItems  {get;set;}
}


//Here is the place where i am not able to get the value of Property : In Controller
[HttpPost]
public ActionResult Index (Payment paymentobj)
{
//Here i am getting payment.ProductItems Value is null  ???

Payment paymentobj1 = new Payment();  //Here Paymentobj1 having all values in this properties
UpdateModel(paymentobj1);  //After executing UpdateModel the paymentobj1 values will be loss.

}

解决方案

When you post to your form the object is lost and all you have is a collection of key value pairs passed in from the web form. What you have to do is create a new object and try to map your form fields to your object. The TryUpdateModel method will do this for you if you created the web form correctly.

your code should look something like this:

[HttpPost]
        public ActionResult Index(FormCollection formData)
        {
            Payment paymentobj1 = new Payment();

            // only allowing data from the form
            if (TryUpdateModel(paymentobj1 , formData))
            {
                
            }

            
        }


这篇关于无法在UpdateModel之后获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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