MVC Ajax在Postback上设置Model Object null [英] MVC Ajax set Model Object null on Postback

查看:37
本文介绍了MVC Ajax在Postback上设置Model Object null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是MVC的新手,在"玩"新应用程序时,我对如何在回发期间保留值感到困惑。 / p>

初始模型包含一个基于Ajax更新设置的对象列表,每个对象包含另一个对象和一个Id。


   ; 公共舱型号

    {

        public List< ContainerObject> ContainerObjects {get; set;}

    }


   公共类ContainerObject

    {

        [ForeighKey(" FieldId")]
$
       公共字段字段{get; set;}

        public int FieldId {get; set;}

       公共字符串标识符{get; set;}       


        public string Value {get; set;}  


    }


所以在ajax上设置了许多List Item以及容器的Field和FieldId。


我正在使用EditorTemplates的Razor对于具有


的ContainerObject    &NBSP;&NBSP;&NBSP; @ Html.HiddenFor(a => a.Field)

        @ Html.HiddenFor(a => a.FieldId)

        @ Html.ValueFor(a => a.Field.Name)

        @ Html.EditorFor(m => m.Identifier)

        @ Html.EditorFor(M => m.Value)&NBSP;&NBSP;


发布表单时,FieldId会正确填充,但实际的Field Object为null,这导致我在条目验证失败并重新加载表单时出现问题。


我做错了什么,你如何坚持对象价值?我真的不想在我的模型中保存完整的字段列表,然后每次重新加载。


非常感谢

解决方案

您好00Stu,


 欢迎使用MSDN论坛支持。


   您更有可能在 http://forums.asp.net  您可以在哪里联系ASP.NET专家。


Hi,

I'm new to MVC and while 'playing' with a new application I'm getting a bit confused on how to retain values during postback.

The initial model contains a list of objects which are set based upon an Ajax update, each object contains another object and an Id.

    public class Model
    {
        public List<ContainerObject> ContainerObjects {get;set;}
    }

    public class ContainerObject
    {
        [ForeighKey("FieldId")]
        public Field Field {get;set;}
        public int FieldId {get;set;}
        public string Identifier {get;set;}       
        public string Value {get;set;}  
    }

So on an ajax a number of List Items are set and the Field and FieldId of the container.

The Razor I'm using EditorTemplates for the ContainerObject which has

        @Html.HiddenFor(a=>a.Field)
        @Html.HiddenFor(a=>a.FieldId)
        @Html.ValueFor(a=>a.Field.Name)
        @Html.EditorFor(m => m.Identifier)
        @Html.EditorFor(m=>m.Value)  

When the form is posted, the FieldId is populated correctly but the actual Field Object is null, which is causing me issues when the entry fails validation and the form is reloaded.

What am I doing wrong and how do you persist an object value? I dont really want to hold a complete list of fields in my Model and then reload each time.

Many Thanks

解决方案

Hi 00Stu,

  Welcome to MSDN Forum Support.

  You are more likely to get more efficient responses to ASP.NET issues at http://forums.asp.net where you can contact ASP.NET experts.


这篇关于MVC Ajax在Postback上设置Model Object null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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