我有一个获取用户名和密码的视图,需要将其传递给数据库。我可以传递用户名而不是密码。
查看:
<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>
}
}
}
答案 0 :(得分:2)
因为您正在创建像这样的密码文本框
@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>
您无法使用锚标记并且没有<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");
}