【问题标题】:How to pass Html.Password helper's value to the Action如何将 Html.Password 助手的值传递给 Action
【发布时间】:2015-07-01 07:27:45
【问题描述】:

我有一个获取用户名和密码并需要将其传递给数据库的视图。我可以传递用户名但不能传递密码。

查看:

<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>
        }
    }

}

【问题讨论】:

  • 你能展示你提交表单时使用的JS代码吗?
  • 没有JS代码。
  • 那么你如何将值传递给操作?通过表单提交 url?

标签: c# html asp.net asp.net-mvc entity-framework


【解决方案1】:

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

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

很明显,您正在使用具有UserName 和Password 属性的视图模型类。我猜它也有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>

您不能使用锚标记和没有&lt;form&gt; 标记来提交页面。您需要有一个&lt;form&gt; 标记和其中的提交按钮。 &lt;form&gt; 标签可以使用 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");
}

【讨论】:

  • 它确实可以作为 TextBoxFor。但是当我尝试实现 PasswordFor 时,它不会将数据传递给操作。
  • 如何生成&lt;form&gt; 标签?你使用Html.BeginForm 助手吗?请出示代码。
猜你喜欢
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多