我正在使用Razor的MVC 5。
我有一个带有文本框和搜索按钮的视图。单击此“搜索”按钮,控制器操作将返回“数据集”。
在同一个视图中,我还有三个文本框,其中显示了值。我想在这些Textbox上显示数据集值。
控制器代码如下:
[HttpGet]
public DataSet Search(string EmpCode)
{
DataSet ds = ExecuteSP("GetEmpDetails", staffCode);
return ds;
}
View标记如下:
@using (Html.BeginForm("Search", "Details",FormMethod.Get))
{
<table style="border:none">
<tr>
<td style="border:none">Enter staff code</td>
<td style="border:none">@Html.TextBox("empcode", "", new { width = "100" })</td>
<td style="border:none"><input type="submit" value="Get Details" /></td>
</tr>
</table>
}
<br /><hr />
<table id="tblEmpDetails" style="border:none">
<tr>
<td style="border:none; width:200px;text-align:left">Employee Name</td>
<td style="border:none">@Html.TextBox("EmpName", "", new { @class = "ReadOnly", width = "200" })</td>
</tr>
<tr>
<td style="border:none;width:200px;;text-align:left">Designation</td>
<td style="border:none;">@Html.TextBox("Designation", "", new { @class="ReadOnly", width = "100" })</td>
</tr>
<tr>
<td style="border:none; width:200px;;text-align:left">Department</td>
<td style="border:none;">@Html.TextBox("Department", "", new { @class = "ReadOnly", width = "100" })</td>
</tr>
</table>
答案 0 :(得分:1)
您需要定义一个可以将视图绑定到的模型,例如
public class EmployeeVM
{
[Display(Name = "Name")]
[Required(ErrorMessage = "Please enter a name")]
public string EmpName { get; set; }
public string Designation { get; set; }
....
}
然后在您的控制器中,初始化视图模型的实例(或视图模型的集合)并将其传递给视图
[HttpGet]
public ActionResult Search(string EmpCode)
{
DataSet ds = ExecuteSP("GetEmpDetails", staffCode);
EmployeeVM model = new EmployeeVM();
// map the first row of the data set properties to the view model
// or each row to a collection of view models
return View(model);
}
在视图中
@model yourAssembly EmployeeWM
@using (Html.BeginForm())
{
....
@Html.LabelFor(m => m.EmpName)
@Html.TextBoxFor(m => m.EmpName)
@Html.ValidationMessageFor(m => m.EmpName)
....
}