gpt4 book ai didi

c# - MVC3- Razor : error while passing query string

转载 作者:行者123 更新时间:2023-11-30 22:27:53 27 4
gpt4 key购买 nike

我是 MVC3 Razor 的新手。任何人都可以帮助我为什么在运行时出现此错误。

错误:对象引用未设置为对象的实例。它在 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" })
}

Controller

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

非常感谢

最佳答案

您还没有为 View 提供模型。

定义一个类作为 View 模型

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

在您的 Controller 的操作中:

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

MVC 的模型绑定(bind)器会自动为参数 model 创建一个实例,并为您将 name 值绑定(bind)到 User.Name

编辑 您的 View 提到了一个名为User 的模型。我更改了答案以反射(reflect)这一点。

关于c# - MVC3- Razor : error while passing query string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11082066/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com