【问题标题】:MVC View is returning null model [duplicate]MVC 视图返回空模型 [重复]
【发布时间】:2015-11-27 21:34:17
【问题描述】:

我不知道为什么它不工作没有什么复杂的。我只是将模型传递给视图,然后从视图中取回模型以进行更新。

控制器:

    [HttpGet]
    [ActionName("Test")]
    public ActionResult Test_get()
    {
        Testing t = new Testing();
        t.a = 100;
        t.name = "ali";
        t.age = "29";
        return View(t);
    }

型号:

public class Testing
{
    public int a = 0;
    public string name;
    public string age;
}

查看:

@using(Html.BeginForm())
{
    @Html.AntiForgeryToken()

     @Html.Label("name")
     @Html.EditorFor(m => m.name)
     @Html.Label("age")
     @Html.EditorFor(m => m.age)    
    <button type="submit" value="Change Text" />
}

这个方法可以取回模型

    [HttpPost]
    [ActionName("Test")]
    [ValidateAntiForgeryToken]
    public ActionResult Test_post(Testing t)
    {
        Testing get_t = new Models.Testing();
        get_t = t;
        return View();
    }

但它返回空模型请帮助我这只是我的项目中的演示。

【问题讨论】:

  • 您的课程Testing 仅包含字段。将它们设为属性,即 public int a { get; set; },以便 DefaultModelBinder 可以设置它们。
  • 哦,真是个小错误.. 非常感谢你让我开心:-)

标签: asp.net-mvc asp.net-mvc-4 model


【解决方案1】:

更新您的模式以使用属性,然后只有 MVC 模型绑定 将起作用

public class Testing
{
    public int a = 0;
    public string name { get; set;} 
    public string age { get; set;} 
}

【讨论】:

  • 谢谢你.. 我为此苦苦挣扎了 2 天 :-)
猜你喜欢
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 2017-05-22
  • 2011-10-15
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多