超文本标记语言文本框工作的地方Html.TextboxFor不# 39;t
本文关键字:TextboxFor Html 文本 工作 超文本标记语言 | 更新日期: 2023-09-27 18:15:41
我是MVC 5和web编程的新手,所以请原谅我。
我有一个视图(用于管理用户角色),其中有三个独立的表单,我或多或少地从教程中复制和粘贴。在本教程中,表单的字段是按以下方式创建的:
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
,但我不确定这是否是真的,无论如何,我想知道如何解决这个问题。
假设表单的模型定义为:
@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" })
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 :)