2012-06-18 33 views
2

我是一个MVC3剃须刀的新手。任何人都可以请帮助我为什么我得到这个错误运行。MVC3 - 剃刀:传递查询字符串时出错

错误: Object reference not set to an instance of an object. 它打破上ActionLink的。

HTML代码:

@model Solution.User 

@using (Html.BeginForm()) 
{ 
    @Html.TextBoxFor(model => model.Name, new {@id = "name-ref", @class = "text size-40"}) 
    @Html.ActionLink("Go Ahead", "Index", "Home", new {name = Model.name, @class = "button" }) 
} 

控制器

[HttpPost] 
public ActionResult Index(string name) 
{ 
    return View(); 
} 

非常感谢

回答

3

您还没有提供一个模型视图。

定义一个类作为视图模型

public class User 
{ 
    public string Name { get; set; } 
} 

而在你的控制器的动作:

[HttpPost] 
public ActionResult Index(User model) 
{ 
    return View(model); 
} 

MVC的模型绑定会自动创建参数model一个实例,并绑定name值到User.Name

编辑您的观点提到了一个名为User的模型。我改变了我的答案以反映这一点。