2013-06-12 53 views
0

我有一个视图名称“编辑”:邮政值到控制器

@model EMMS.ViewModels.MaintenanceViewModel 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    <fieldset style="width: 800px;"> 
     <legend>Maintenance Details</legend> 
     <div class="editor"> 
      @Html.LabelFor(model => model.EquipmentID) 
      @Html.TextBoxFor(model => model.EquipmentID, new { style = "width: 200px;" }) 
      @Html.ValidationMessageFor(x => x.EquipmentID) 
     </div> 
    </fieldset> 
    <div id="dialog-validate-user" style="display: none;"> 
     @Html.Partial("_ValidateUser") 
    </div> 
} 

它包含了与下面的局部视图填充一个jQuery UI的对话框:

@{ 
    ViewBag.Title = "_ValidateUser"; 
} 

<h3>User Verification Required</h3> 
<p>Please enter your network login credentials.</p> 
@using (Html.BeginForm("Edit", "Maintenance", FormMethod.Post)) { 
    <div> 
     <p> 
      <span>User Name:</span> 
      @Html.TextBox("UserName") 
     </p> 
     <p> 
      <span>Password:</span> 
      @Html.Password("Password") 
     </p> 
    </div> 
} 

我想要将用户名和密码输入的值传递回维护控制器的编辑(发布)操作结果。其行为结果如下所示:

[HttpPost] 
public ActionResult Edit(MaintenanceViewModel maintenanceViewModel, string UserName, string Password) 
{ 
} 

当我断点ActionResult时,UserName和Password始终为空。它们不是视图模型的一部分,只是我需要传递给编辑方法的参数。

任何帮助表示赞赏

+0

在生成的HTML输出中,您的用户名和密码文本框的实际名称是什么? –

+0

以下是输出:

User Name

Password

steveareeno

+0

这看起来对我来说很合适。您能否验证数据实际上是从浏览器发送的?你可以检查HTTP请求来验证吗? –

回答

3

重要:

当您在该div使用jQuery UI的对话框,它是从表单中删除。这就是为什么他们是空的。

您可以在我的jsFiddle here中看到。

如果我是你,我会分开表格,以便它们不嵌套。

编辑观点:

@model EMMS.ViewModels.MaintenanceViewModel 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset style="width: 800px;"> 
     <legend>Maintenance Details</legend> 
     <div class="editor"> 
      @Html.LabelFor(model => model.EquipmentID) 
      @Html.TextBoxFor(model => model.EquipmentID, new { style = "width: 200px;" }) 
      @Html.ValidationMessageFor(x => x.EquipmentID) 
     </div> 
    </fieldset> 
} 
<div id="dialog-validate-user" style="display: none;"> 
    @Html.Partial("_ValidateUser") 
</div> 

_ValidateUser部分:

@{ 
    ViewBag.Title = "_ValidateUser"; 
} 

<h3>User Verification Required</h3> 
<p>Please enter your network login credentials.</p> 
@using (Html.BeginForm("Login", "Maintenance", FormMethod.Post)) 
{ 
    <div> 
     <p> 
      <span>User Name:</span> 
      @Html.TextBox("UserName") 
     </p> 
     <p> 
      <span>Password:</span> 
      @Html.Password("Password") 
     </p> 
    </div> 
} 

控制器:

[HttpPost] 
public ActionResult Login(string UserName, string Password) 
{ 
    // do something here with the login 
} 

MVC的美是恰当的分离。

请注意,自定义ViewModel的要点是将这些字段组合在一起。

此外:

才能上的要求是什么一点点的信息后,在这里最好的解决办法是去除部分的形式和用户名和密码添加到视图模型:)

+0

您的意思是控制器中的局部视图有不同的操作结果?如果是这样,我仍然需要将用户名/密码传递给编辑方法post actionresult。基本上,我有一个要求在保存数据之前重新认证用户,用户名必须与数据一起保存,审计功能的种类 – steveareeno

+0

是的,这就是我的意思。您可以像我所显示的那样使用分隔,或者使用viewModel的用户名和密码部分。 – technicallyjosh

+0

@steveareeno我编辑了我的答案。 – technicallyjosh