asp.net-mvc如何将 Html.Password helper 值传递到行动

发布时间: 2017/3/25 0:55:25
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个看法,获取用户名和密码,需要将它传递给数据库。我可以传递用户名而不是密码。

视图︰

<div class="form-group">
    @Html.LabelFor(m => m.UserName, new { @class = "label-control" })
    @Html.PasswordFor(m => m.UserName, new { @class = "form-control", autofocus = "", value = "Text" })
    @Html.ValidationMessageFor(m => m.UserName, null, new { @class = "alert-danger" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Password, new { @class = "label-control" })
    @Html.TextBoxFor(m => m.Password, new { @class = "form-control" })
    @Html.ValidationMessageFor(m => m.Password, null, new { @class = "alert-danger" })
</div>

控制器︰

public ActionResult EditUserSubmit(string UserName, string Password, string ProcessType)
{
    string process;
    string name = Url.RequestContext.RouteData.Values["id"].ToString();
    process = (ProcessType == "Processed") ? "P" : "B";
    DB.Entities.user users = db.users.Where(m => m.username == name).FirstOrDefault();
    users.password = Password;
    users.processtype = process;
    db.SaveChanges();

    return RedirectToAction("Manager");
}

更新

这是整个视图

@model NV.Tax.SST.Gateway.MVC.Models.AdministrationModel
@{string name = Url.RequestContext.RouteData.Values["id"].ToString();}

<div class="x-form-wrapper">
    <div class="x-form-title">
        <h3><strong>User Detail for <b>@Url.RequestContext.RouteData.Values["id"]</b></strong></h3>
    </div>
    <div class="x-form-body">
        <div class="form-group">
            @Html.LabelFor(m => m.UserName, new { @class = "label-control" })
            @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", autofocus = "", value = "Text" })
            @Html.ValidationMessageFor(m => m.UserName, null, new { @class = "alert-danger" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "label-control" })
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Password, null, new { @class = "alert-danger" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.ProcessType, new { @class = "label-control" })
            @Html.DropDownListFor(m => m.ProcessType, new[]{
                    new SelectListItem { Text = "Processed", Value = "Processed" },
                    new SelectListItem { Text = "Both", Value = "Both" }
                }, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.ProcessType, null, new { @class = "alert-danger" })
        </div>
        <div class="form-group">
            <a href="/Administration/EditUserSubmit/@name" class="btn btn-lg btn-primary">Save</a>
            @Html.ActionLink("Cancel", "Manager", "Administration", null, new { @class = "btn btn-lg btn-default" })
        </div>
    </div>
</div>
@{
    string password = "";
    var entity = new NV.Tax.SST.Gateway.DB.Entities.sstpEntities();
    var data = from db in entity.users
                where db.username == name
                select db;
    foreach(var item in data)
    {
        password = item.password;
        <script>document.getElementById("Password").defaultValue = "@password"</script>
        if (item.processtype == "B")
        {
            <script>document.getElementById("ProcessType").value = "Both"</script>
        }
        else
        {
            <script>document.getElementById("ProcessType").value = "Processed"</script>
        }
    }

}

解决方法 1:

因为您正在创建这样的密码文本框

@Html.TextBoxFor(m => m.Password, new { @class = "form-control" })

很显然,您使用了视图模型类 UserNamePassword 属性。我猜,它也有 ProcessType 属性。既然你有下面的语法在视图的顶部

@model NV.Tax.SST.Gateway.MVC.Models.AdministrationModel

您可以使用 NV.Tax.SST.Gateway.MVC.Models.AdministrationModel 作为您的控制器操作方法如下图所示的参数

public ActionResult EditUserSubmit(NV.Tax.SST.Gateway.MVC.Models.AdministrationModel model)
{
    string process;
    string name = Url.RequestContext.RouteData.Values["id"].ToString();
    process = (model.ProcessType == "Processed") ? "P" : "B";
    DB.Entities.user users = db.users.Where(m => m.username == name).FirstOrDefault();
    users.password = model.Password;
    users.processtype = process;
    db.SaveChanges();

    return RedirectToAction("Manager");
}

model.Password 将是您在密码文本框中输入。

编辑

看后整个视图代码,这是你哪里错了

<a href="/Administration/EditUserSubmit/@name" class="btn btn-lg btn-primary">Save</a>

你不能提交使用锚点标记的页和没有 <form> 标记。你需要有 <form> 标记和里面一个提交按钮。<form>可以使用生成的标记 Html.BeginForm 帮助器方法。更改为下面的视图代码

@model NV.Tax.SST.Gateway.MVC.Models.AdministrationModel
@{string name = Url.RequestContext.RouteData.Values["id"].ToString();}

<div class="x-form-wrapper">
    <div class="x-form-title">
        <h3><strong>User Detail for <b>@Url.RequestContext.RouteData.Values["id"]</b></strong></h3>
    </div>
    <div class="x-form-body">
        @using (Html.BeginForm("EditUserSubmit", "Administration"))
        {
        <div class="form-group">
            @Html.LabelFor(m => m.UserName, new { @class = "label-control" })
            @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", autofocus = "", value = "Text" })
            @Html.ValidationMessageFor(m => m.UserName, null, new { @class = "alert-danger" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "label-control" })
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Password, null, new { @class = "alert-danger" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.ProcessType, new { @class = "label-control" })
            @Html.DropDownListFor(m => m.ProcessType, new[]{
                    new SelectListItem { Text = "Processed", Value = "Processed" },
                    new SelectListItem { Text = "Both", Value = "Both" }
                }, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.ProcessType, null, new { @class = "alert-danger" })
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-lg btn-primary">Save</button>
            @Html.ActionLink("Cancel", "Manager", "Administration", null, new { @class = "btn btn-lg btn-default" })
        </div>
        }
    </div>
</div>
@{
    string password = "";
    var entity = new NV.Tax.SST.Gateway.DB.Entities.sstpEntities();
    var data = from db in entity.users
                where db.username == name
                select db;
    foreach(var item in data)
    {
        password = item.password;
        <script>document.getElementById("Password").defaultValue = "@password"</script>
        if (item.processtype == "B")
        {
            <script>document.getElementById("ProcessType").value = "Both"</script>
        }
        else
        {
            <script>document.getElementById("ProcessType").value = "Processed"</script>
        }
    }

}

您还应当添加 [HttpPost] 属性和更改您的控制器操作方法如下图所示

[HttpPost]
public ActionResult EditUserSubmit(NV.Tax.SST.Gateway.MVC.Models.AdministrationModel model)
{
    string process = (model.ProcessType == "Processed") ? "P" : "B";
    DB.Entities.user users = db.users.Where(m => m.username == model.UserName).FirstOrDefault();
    users.password = model.Password;
    users.processtype = process;
    db.SaveChanges();

    return RedirectToAction("Manager");
}
官方微信
官方QQ群
31647020