我对MVC 5和网络编程都很陌生,所以请耐心等待。
我有一个视图(用于管理用户角色),我有三个单独的表单,我或多或少地从教程中复制和粘贴。在本教程中,表单的字段按以下方式创建:
Username : @Html.TextBox("Username")
由于我希望样式适合他们,我将代码更改为更像MVC 5模板中的默认表单,所以最终看起来像这样:
@Html.LabelFor(m => m.GetRolesUsername, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.GetRolesUsername, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.GetRolesUsername, "", new { @class = "text-danger" })
</div>
我的模型ManageUserRolesViewModel
看起来像这样(请注意,在我的视图顶部,我有@model ManageUserRolesViewModel
):
public class ManageUserRolesViewModel
{
#region Assign Role
[Required]
[Display(Name = "Username", ResourceType = typeof(Resources))]
public string AssignRoleUsername { get; set; }
[Required]
[Display(Name = "RoleName", ResourceType = typeof(Resources))]
public string AssignRoleRole { get; set; }
#endregion
#region Get Roles
[Required]
[Display(Name = "Username", ResourceType = typeof(Resources))]
public string GetRolesUsername { get; set; }
#endregion
#region Unassign Role
[Required]
[Display(Name = "Username", ResourceType = typeof(Resources))]
public string UnassignRoleUsername { get; set; }
[Required]
[Display(Name = "RoleName", ResourceType = typeof(Resources))]
public string UnassignRoleRole { get; set; }
#endregion
}
请注意我是如何使用注释直接从资源加载ViewModel中元素的名称的。我这样做是出于本地化的目的,资源正在用西班牙语返回字符串。 我认为这可能是我问题的根源,但我不确定。
然后,在我的控制器中,我有以下方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult GetRoles(string UserName)
{
if (!string.IsNullOrWhiteSpace(UserName))
{
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
ViewBag.RolesForThisUser = this.UserManager.GetRoles(user.Id);
ViewBag.Roles = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
}
return View("ManageUserRoles");
}
现在,发生了什么:如果我使用Username : @Html.TextBox("Username")
,当在控制器中调用方法GetRoles()
时,UserName
参数就在那里并且用户成功加载。相反,我使用
@Html.LabelFor(m => m.GetRolesUsername, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.GetRolesUsername, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.GetRolesUsername, "", new { @class = "text-danger" })
</div>
调用方法时,UserName
参数为null
。
我的猜测是,代码MVC中的某处正在寻找UserName
或Username
并找到Usuario
,但我不确定这是否属实,无论如何,我想知道如何解决这个问题。
答案 0 :(得分:3)
假设您的表单将模型定义为:
@model ManageUserRolesViewModel
和视图中的某个地方:
@Html.TextBoxFor(m => m.GetRolesUsername, new { @class = "form-control" })
行动应如下所示:
[HttpPost]
public ActionResult GetRoles(ManageUserRolesViewModel vm)
{
string userName = vm.GetRolesUsername;
//rest of code omitted.
}
因此,您不依赖于UserName
参数,并且可以使用视图模型本身。
如果这还不够,你可以这样做:
@Html.TextBoxFor(x => x.GetRolesUsername, new { Name = "UserName" })
答案 1 :(得分:0)
Hi Eric,
@Html.TextBox("Username")
// It creates a html input element with name "Username"
<input type="text" id=""Username" name="Username" value="" />
And
@Html.TextBoxFor(m => m.GetRolesUsername)
// It also create a html element with name as it's property name. ie, "GetRolesUsername"
<input type="text" id=""Username" name="Username" value="@Model.GetRolesUsername" />
So, if you submit your form, your browser will send parameters as,
Username = "value in the @html.TeaxtBox()",
GetRolesUsername = "value in the @html.TextBoxFor()"
So, both values will be passed to your MVC controller. Then you can decide what parameters you want to receive.
public ActionResult Submit(string Username, string GetRolesUsername)
{
// You can get Username and GetRolesUsername here
}
public ActionResult Submit(string Username)
{
// You tell your controller that I am expecting only one parameter and that is Username
}
Then there will be a main difference between `@html.TextBox()` and `@html.TextBoxFor()' is,
`@html.TextBox()` will just create a text element. But, `@html.TextBoxFor()' will create and set value to the element.
Hope this will help you.
If you have any doubts, please feel free to ask me.
**And Grand Welcome To MVC.**
I am also just a beginner in MVC :)