Asp.net mvc ASP MVC5列表/编辑/查看用户
我只是尝试创建一个UserController,它显示应用程序用户的列表。具有编辑/查看和删除功能 问题是,当我在模型应用程序中使用item.Id字段时,会显示一些长Id,而这些长Id在url中不被接受为Id参数。当我单击“编辑”时,会显示以下消息:参数字典包含不可为null的类型“System.Int32”的参数“Id”的null条目,用于方法“System.Web.Mvc.ActionResult edit”(Int32)'在'DesignCrew.Controller.UserController'中。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数aam:参数 那么,我该如何解决这个问题呢。或者在ASP MVC5中有更好的用户管理方法吗 用户控制器:Asp.net mvc ASP MVC5列表/编辑/查看用户,asp.net-mvc,simplemembership,Asp.net Mvc,Simplemembership,我只是尝试创建一个UserController,它显示应用程序用户的列表。具有编辑/查看和删除功能 问题是,当我在模型应用程序中使用item.Id字段时,会显示一些长Id,而这些长Id在url中不被接受为Id参数。当我单击“编辑”时,会显示以下消息:参数字典包含不可为null的类型“System.Int32”的参数“Id”的null条目,用于方法“System.Web.Mvc.ActionResult edit”(Int32)'在'DesignCrew.Controller.UserContro
public ActionResult Index()
{
var context = new ApplicationDbContext();
var user = context.Users.ToList();
return View(user);
}
Index.cshtml:
@model List<DesignCrew.Models.ApplicationUser>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table-users">
<tr>
<th style="width: 400px;">Naam</th>
<th>Opties</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayName(item.UserName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
@型号列表
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“新建”、“创建”)
纳姆
选择权
@foreach(模型中的var项目)
{
@Html.DisplayName(item.UserName)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
我知道您希望逐个显示您的用户进行编辑
[HttpGet]
public ActionResult Edit(int? id)
{
var context = new ApplicationDbContext();
var user = context.Users.Find(id);
return View(user);
}
您的UserController编辑方法需要Int32参数,您正在尝试使用out参数访问它。还显示控制器的编辑方法,您的参数名是“id”还是什么?它看起来是这样的:public ActionResult Edit(int id){return View();}实际上simplemembership应用程序用户不使用id作为唯一键,而是使用用户名,所以更改@Html.ActionLink(“编辑”,“编辑”,新建){UserName=item.UserName})和controll方法来编辑(字符串UserName){return View();}Hi ijaz。使用该控制器代码,在使用@Html.EditorFor(model=>model.UserName)呈现的输入字段中不会设置初始值。我需要在反操作方法中传递对象吗?抱歉,我是asp.net mvc的新手。顺便说一句,用户名现在在url/user/Edit/username中提供。我找到了一个解决方案。将id字段保留在url中,并将操作方法参数“id”更改为类型“string”使其工作。我不确定这在近6年后是否有用。还有“仅代码”答案对任何可能在这个问题上绊倒的人都没有真正的帮助。