Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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
Asp.net mvc 3 查看返回空模型asp.net mvc3_Asp.net Mvc 3_Model - Fatal编程技术网

Asp.net mvc 3 查看返回空模型asp.net mvc3

Asp.net mvc 3 查看返回空模型asp.net mvc3,asp.net-mvc-3,model,Asp.net Mvc 3,Model,我尝试在局部视图中初始化DataView.Model。页面工作正常,但当我返回控制器时,模型为空 一些帮助(解决方案或解释它不正确的原因)。 谢谢 代码: 在我看来: ViewData.Model = new DiamondPrint(); ViewData.Model.Diamond = m_db.DiamondInfoes.Where(di => di.Id == id).SingleOrDefault(); 在我的控制器中: public ActionResult Previe

我尝试在局部视图中初始化DataView.Model。页面工作正常,但当我返回控制器时,模型为空

一些帮助(解决方案或解释它不正确的原因)。 谢谢

代码:

在我看来:

 ViewData.Model = new DiamondPrint();
 ViewData.Model.Diamond = m_db.DiamondInfoes.Where(di => di.Id == id).SingleOrDefault();
在我的控制器中:

public ActionResult Preview(DiamondPrint d)//the properties in d = null
{
   return View(d);
}

这是一篇关于模型绑定的好文章。确保您正在html输入字段中设置name属性

查看您包含的代码,您似乎正在初始化部分视图中的ViewData.Model,但在控制器操作中,您希望默认的模型绑定器重新创建您的模型。要使模型绑定器重新创建模型,您需要创建强类型视图

例如:

控制器:

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(IndexModel model)
{
    return View();
}
public class IndexModel
{
    public string MyValue { get; set; }
}
@model MvcApplication14.Models.IndexModel

@Html.EditorFor(m => m.MyValue)
型号:

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(IndexModel model)
{
    return View();
}
public class IndexModel
{
    public string MyValue { get; set; }
}
@model MvcApplication14.Models.IndexModel

@Html.EditorFor(m => m.MyValue)
查看:

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(IndexModel model)
{
    return View();
}
public class IndexModel
{
    public string MyValue { get; set; }
}
@model MvcApplication14.Models.IndexModel

@Html.EditorFor(m => m.MyValue)
注意顶部的@model定义(忽略命名空间)