Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 请求中缺少属性时的DefaultModelBinder行为_C#_Asp.net Mvc_Asp.net Mvc 3_Model Binding - Fatal编程技术网

C# 请求中缺少属性时的DefaultModelBinder行为

C# 请求中缺少属性时的DefaultModelBinder行为,c#,asp.net-mvc,asp.net-mvc-3,model-binding,C#,Asp.net Mvc,Asp.net Mvc 3,Model Binding,我有一个如下的模型: public class TestViewModel { string UpdateProperty { get; set; } string IgnoreProperty { get; set; } ComplexType ComplexProperty { get; set; } } 在哪里 我的控制器操作: public Edit(int id, FormColleciton formCollection) { var model = service

我有一个如下的模型:

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

我的控制器操作:

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

  //...
}
调用编辑操作时,我有一个formCollection参数,它只包含UpdateProperty的键/值

正确设置对TryUpdateModel UpdateProperty的调用后,IgnoreProperty将保持不动状态,但ComplexProperty将设置为null,即使它以前有一个值

TryUpdateModel()是否应该只修改作为请求一部分的属性?如果情况并非如此,那么解决此问题的最佳方法是什么?因此,只有当请求中包含ComplexProperty时,才会对其进行修改


在Darin指出上面的测试用例没有演示问题之后,我添加了一个场景,其中该问题确实发生了:

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; }
}
更新SubModel.StringOne属性,但将ComplexTypeOne设置为null,即使请求中未包含该属性


这是预期的行为吗(假设这种情况不会发生,除非使用复杂类型的可枚举项)?如何最好地解决这个问题?

您的测试用例一定有问题,因为我无法复制它。以下是我尝试过的:

模型(请注意,我使用公共属性):

控制器:

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();
    }
}
现在,我发送以下请求:
/home/index?UpdateProperty=abc
,在条件中,只有UpdateProperty使用查询字符串中的新值进行修改。所有其他属性(包括复杂属性)保持不变


还请注意,
FormCollection
操作参数是无用的。

将对此进行研究-在简化到此处发布时,我可能错过了问题的原因。(我留下FormCollection参数以查看请求中的值)感谢您确认默认模型活页夹应保持该值不变。我修改了我的帖子,添加了一个出现问题的示例。我在此处发布了一个新问题和一个工作示例:
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
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();
    }
}