DefaultModelBinder行为时,财产的请求缺席 [英] DefaultModelBinder behaviour when property absent from request

查看:127
本文介绍了DefaultModelBinder行为时,财产的请求缺席的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似下面的模型:

public class TestViewModel
{
  string UpdateProperty { get; set; }
  string IgnoreProperty { get; set; }
  ComplexType ComplexProperty { get; set; }
}

其中,

public class ComplexType
{
  long? Code { get; set; }
  string Name { get; set; }
}

我的控制器动作:

My controller action:

public Edit(int id, FormColleciton formCollection)
{
  var model = service.GetModel(id);
  TryUpdateModel(model);

  //...
}

当调用编辑动作我只包含一个键/值对UpdateProperty一个的FormCollection参数。

When calling the Edit action I have a formCollection parameter containing only a key/value for UpdateProperty.

要TryUpdateModel UpdateProperty呼叫设置正确后,IgnoreProperty保留未触及,但ComplexProperty被设置为null,即使previously有一个值。

After the call to TryUpdateModel UpdateProperty is set correctly, IgnoreProperty is left un-touched but ComplexProperty is set to null, even if it previously had a value.

应该TryUpdateModel()只修改那些请求的一部分属性?如果这不是什么来解决它的最好方法,所以如果它被包含在请求ComplexProperty只修改了呢?

Should TryUpdateModel() only modify properties that are a part of the request? If this is not the case what is the best way to work around this so ComplexProperty is only modified if it is included in the request?

有人指出,后达林说上面的测试用例中,其中这个问题真的发生时并没有表现出我添加了一个场景中的问题:

After it was pointed out by Darin that the test case above didn't demonstrate the problem I have added a scenario where this problem really occurs:

public class TestViewModel
{
    public List<SubModel> List { get; set; }
}

public class SubModel
{
    public ComplexType ComplexTypeOne { get; set; }
    public string StringOne { get; set; }
}

public class ComplexType
{
    public long? Code { get; set; }
    public string Name { get; set; }
}

控制器动作:

public ActionResult Index()
{
    var model = new TestViewModel
                    {
                        List = new List<SubModel> { 
                            new SubModel{
                                ComplexTypeOne = new ComplexType{Code = 1, Name = "5"},
                                StringOne = "String One"
                            } 
                        }
                    }; 

    if (TryUpdateModel(model)) { } 

    return View(model);
}

发送该请求:

/Home/Index?List[0].StringOne=test

更新SubModel.StringOne属性而设置ComplexTypeOne为空,即使它不包含在请求中。

updates SubModel.StringOne property but sets ComplexTypeOne to null, even though it is not included in the request.

这是预期的行为(给这不会发生,除非复杂类型的枚举使用)?如何最好地解决此问题?

Is this expected behaviour (given this does not happen unless an enumerable of complex types is used)? How best to work around this?

推荐答案

有一定有什么不对您的测试情况下我无法重现它。下面是我的尝试:

There must be something wrong with your test case as I was unable to reproduce it. Here's what I tried:

模式(请注意,我用的公共属性):

Model (notice that I use public properties):

public class TestViewModel
{
    public string UpdateProperty { get; set; }
    public string IgnoreProperty { get; set; }
    public ComplexType ComplexProperty { get; set; }
}

public class ComplexType
{
    public long? Code { get; set; }
    public string Name { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new TestViewModel
        {
            IgnoreProperty = "to be ignored",
            UpdateProperty = "to be updated",
            ComplexProperty = new ComplexType
            {
                Code = 1,
                Name = "5"  
            }
        };

        if (TryUpdateModel(model))
        {

        }
        return View();
    }
}

现在我发送以下请求:<?code> /家庭/指标UpdateProperty = ABC ,只有UpdateProperty修改与查询字符串的新值的条件内。所有其他属性,包括复杂的财产,都保持不变。

Now I send the following request: /home/index?UpdateProperty=abc and inside the condition only the UpdateProperty is modified with the new value from the query string. All other properties, including the complex property, are left untouched.

另外请注意,的FormCollection 操作参数是没用的。

Also notice that the FormCollection action parameter is useless.

这篇关于DefaultModelBinder行为时,财产的请求缺席的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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