C# 如何在ASP.NET MVC中将ObjectViewModel从视图发送到控制器?
我正在使用MVC模板在ASP.NET Web应用程序中工作 我正在尝试将ObjectViewModel从View.cshtml发送或传递到控制器,但在控制器中,ObjectViewModel为null 其思路如下: 我使用RegisterViewModel类型的对象集合显示视图IndexUsersInActiveDirectory.cshtml。在该视图中有一个@Html.ActionLink,用于将集合的元素发送到注册控制器: IndexUsersInActiveDirectory.csthmlC# 如何在ASP.NET MVC中将ObjectViewModel从视图发送到控制器?,c#,asp.net,razor,controller,C#,Asp.net,Razor,Controller,我正在使用MVC模板在ASP.NET Web应用程序中工作 我正在尝试将ObjectViewModel从View.cshtml发送或传递到控制器,但在控制器中,ObjectViewModel为null 其思路如下: 我使用RegisterViewModel类型的对象集合显示视图IndexUsersInActiveDirectory.cshtml。在该视图中有一个@Html.ActionLink,用于将集合的元素发送到注册控制器: IndexUsersInActiveDirectory.csthm
@model IEnumerable<SecureEscuelaWithIdentity.Models.RegisterViewModel>
@{
ViewBag.Title = "IndexUsersInActiveDirectory";
}
<h2>IndexUsersInActiveDirectory</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.ActionLink("Select", "Register", new { item })
</td>
</tr>
}
</table>
RegisterViewModel类是:
public class RegisterViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
}
视图Register.cshtml
@model SecureEscuelaWithIdentity.Models.RegisterViewModel
@{
ViewBag.Title = "Register";
}
<h2>@ViewBag.Title.</h2>
@using (Html.BeginForm("RegisterInApp", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
@Html.ValidationSummary()
<li>@Html.ActionLink("ActiveDirectory", "IndexUsersInActiveDirectory", "Account")</li>
<div class="form-group">
@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model securescuelawithidentity.Models.RegisterViewModel
@{
ViewBag.Title=“寄存器”;
}
@ViewBag.Title。
@使用(Html.BeginForm(“RegisterInApp”,“Account”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@Html.AntiForgeryToken()
创建一个新帐户。
@Html.ValidationSummary()
@ActionLink(“ActiveDirectory”、“IndexUsersInActiveDirectory”、“帐户”)
@LabelFor(m=>m.UserName,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.UserName,新的{@class=“form control”})
@LabelFor(m=>m.Email,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.Email,新的{@class=“form control”})
}
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
如何将整个集合元素(作为模型)发送到控制器?必须使用“for”循环迭代元素,如for循环 (int i;i有两个问题 1) ActionLink的第4个参数缺少空值 如果没有第4个参数,则假定第3个参数是HTML属性,而不是querystring参数 我发现这个小小的第四个参数“怪癖”是MVC中最烦人的覆盖之一 2) 您的
项
需要是可以通过querystring传递的简单数据值
由于项目不是简单的值类型,因此需要传回其唯一值。e、 g:
@Html.ActionLink("Select", "Register", new { name = item.Name, email = item.Email }, null)
操作链接不进行回发,它们只是在页面中创建锚链接,因此必须在查询字符串中传递任何值
将接收操作更改为匹配:
[Authorize(Roles = "Admin")]
public ActionResult Register(string name, string email)
{
RegisterViewModel model = new RegisterViewModel()
{
Name = name,
Email = email
};
return View(model);
}
但我将这些细节留给您:)TrueBlueAussie提出的解决方案适用于在浏览器地址栏中显示查询字符串没有问题的情况 如果是这样,@Html.ActionLink可以是这样的:
@Html.ActionLink("Select", "Register", new {item}, null)
[Authorize (Roles = "Admin")]
public ActionResult Register (RegisterViewModel model)
{
return View (model);
}
因为控制器将“item”作为RegisterViewModel对象(尽管实际上是一个querystring)。注意:不要忘记第四个参数,该参数导致TrueBlueAussie“null”
寄存器控制器如下所示:
@Html.ActionLink("Select", "Register", new {item}, null)
[Authorize (Roles = "Admin")]
public ActionResult Register (RegisterViewModel model)
{
return View (model);
}
它起作用了
如上所述,这在Register视图中显示querystring,这在我的实现中是不正确的。我不想在地址栏中显示此查询字符串
为了实现这一点,我添加了一个中间控制器,负责接收“item”(来自IndexUsersInActiveDirectory.csthml)作为RegisterViewModel对象(实际上是一个querystring),并使用一个名为“model”的键将该对象添加到dictionary中
以下是控制器寄存器temp:
[Authorize (Roles = "Admin")]
public ActionResult RegisterTemp (RegisterViewModel model)
{
TempData ["model"] = model;
return RedirectToAction ("Register");
}
在“IndexUsersInActiveDirectory.csthml”视图中,@ActionLink指向控制器寄存器而不是控制器寄存器,如下所示:
@ Html.ActionLink ("Select", "RegisterTemp", new {item}, null)
重要的是,RegisterTemp返回一个“RedirectToAction”来注册视图
最后,当RegisterTemp控制器将RedirectToAction返回到RegisterController时,它从TempData字典中获取具有“model”键的元素,以便将其返回到视图。因此,寄存器控制器是:
//
// GET: / Account / Register
[Authorize (Roles = "Admin")]
public ActionResult Register ()
{
return View (TempData ["model"]);
}
我提到,@TrueBlueAussie提出的解决方案对于显示查询字符串并不重要的场景是完全有效的
我刚才详细介绍的解决方案是避免显示查询字符串
如果有更正确的解决方案来解决我的场景,欢迎。否。在这个问题中,foreach循环中没有编辑输入。问题在于ActionLinks。