我有一个像这样声明的PaartialView:
@model IEnumerable<mvc1.Models.ProjectDetailModel>
@using (Html.BeginForm())
<form method="get" action="EditProject" enctype="multipart/form-data">
<br />
<fieldset>
<legend>Project Detail</legend>
@foreach (var item in Model)
{
<tr>
<th class="thdetail">
Project Code
</th>
<td class="tddetail">
@Html.DisplayFor(modelItem => item.projectCode)
</td>
<tr>
<th class="thdetail">
Project Name
</th>
<td class="tddetail">
@Html.DisplayFor(modelItem => item.projectName)
</td>
</tr>
<tr>
<th class="thdetail">
Project Type
</th>
<td class="tddetail">
@Html.DisplayFor(modelItem => item.projectType)
</td>
</tr>
<tr>
<th class="thdetail">
Detailed Description
</th>
<td class="tddetail">
<div style="height: 100px; width:700px; overflow: scroll">
@Html.DisplayFor(modelItem => item.projectDescription)
</div>
</td>
</tr>
</table>
</fieldset>
<input type="submit" value="Edit" />
</form>
}
在提交按钮上,我正在调用一个控制器,但是当它进入控制器时,模型没有被传递回控制器。如何将模型返回到控制器,甚至只有1个字段,即。 Model.projectCode是主键
在控制器中我有fll接受模型并获取主键并调用存储过程将结果返回到另一个VIEW()
[HttpGet]
public ActionResult EditProject(ProjectDetailModel model)
{
DBController dbcontroller = new DBController();
string l_user_name = SessionBag.Current.UserName;
Int64 l_project_code = model.projectCode;
if (dbcontroller.DBConnection())
{
MySqlCommand command = new MySqlCommand("edit_projects", dbcontroller.conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
// Input parameters for the insert_projects STORED PROC
command.Parameters.Add(new MySqlParameter("userName", SessionBag.Current.UserName));
command.Parameters["@userName"].Direction = System.Data.ParameterDirection.Input;
// Output parameters for the view_sr_projects_detail STORED PROC
command.Parameters.Add(new MySqlParameter("projectName", MySqlDbType.LongText));
command.Parameters["@projectName"].Direction = System.Data.ParameterDirection.Output;
command.Parameters.Add(new MySqlParameter("projectType", MySqlDbType.LongText));
command.Parameters["@projectType"].Direction = System.Data.ParameterDirection.Output;
command.Parameters.Add(new MySqlParameter("projectDescription", MySqlDbType.LongText));
command.Parameters["@projectDescription"].Direction = System.Data.ParameterDirection.Output;
try
{
MySqlDataReader rdr = command.ExecuteReader();
var model1 = new ProjectDetailModel();
while (rdr.Read())
{
model1.projectCode = (Int64)(rdr["projectCode"]);
model1.projectName = rdr["projectName"].ToString();
model1.projectType = rdr["projectType"].ToString();
model1.projectDescription = rdr["projectDescription"].ToString();
}
dbcontroller.conn.Close();
return View(model1);
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
dbcontroller.conn.Close();
ViewBag.Message = "Could not view your detail project. Error " + ex.Number + " has ocurred. Please try again or contact the system administrator.";
return View("Error");
}
}
else
{
ViewBag.Message = "Could not connect to the database. Please try again or contact the system administrator";
return View("Error");
}
}
我的模型看起来像这样:
public class ProjectDetailModel
{
[Display(Name = "Project Code")]
public Int64 projectCode { get; set; }
[Display(Name = "User Name")]
public string srUserName { get; set; }
[Display(Name = "Project Name")]
public string projectName { get; set; }
[Display(Name = "Project Type")]
public string projectType { get; set; }
[Display(Name = "Project Requirement")]
public string projectDescription { get; set; }
}
public class ProjectDetailModelList : List<ProjectDetailModel>
{
}
感谢 那仁
答案 0 :(得分:1)
这是你可以做的。下面只是部分代码,修改它以适应您的示例:
您视图中的代码:
<button id="btnEdit" type="button">Edit</button>
然后我使用jQuery
向按钮添加一个单击侦听器(确保将jQuery添加到视图中):
<script>
$(document).ready(function () {
$('#btnEdit').click(function () {
window.location = '@Url.RouteUrl(new { action = "EditProject", projectCode = Model.projectCode })';
});
});
</script>
你的行动方法:
public ActionResult EditProject(int projectCode)
{
// Retrieve this specific project using this code
// Do what needs to be done to populate the required input fields on view
}
这是我发现的最佳方式。
答案 1 :(得分:0)
当MVC调用您的controller.EditProject(ProjectDetailModel)
操作时,绑定系统会尝试从发布到页面的数据(或查询字符串或路由数据等)中创建ProjectDetailModel
对象。为了让它为您创建和填充ProjectDetailModel
,您必须具备以下条件:
ProjectDetailModel
所以如果ProjectDetailModel
看起来像这样:
public class ProjectDetailModel
{
public ProjectDetailModel()
{
}
public int ProjectId
{
get;
set;
}
}
...您可以通过发布此表单来填充MVC:
<form method="get" action="EditProject" enctype="multipart/form-data">
<input type="hidden" id="ProjectId" name="ProjectId" value="123" />
</form>
修改强>
查看您在问题中添加的额外代码,您需要做一些事情来解决:
@using (this.Html.BeginForm())
将呈现一个打开的表单标记 - 因为您手动编写了一个不需要该行的标记。
使用this.Html.DisplayFor()
只会将属性值写入屏幕;如果要将其填充到操作中的参数中,则需要将值提供给绑定系统。您可以使用EditorFor()
代替DisplayFor()
来执行此操作,这会导致它们在发送到操作时包含在请求中。