我想在MVC4中进行编辑 [英] I wanna perform edit in MVC4

查看:67
本文介绍了我想在MVC4中进行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在列表绑定在视图中所以我有相同视图中的页面加载时的详细信息我想在我点击编辑按钮时执行编辑





这是我用来在页面加载时显示细节的动作

public ActionResult EditEmployee(int id = 0)

{

返回视图(db.Employees.Where(o => o.EmployeeID == id).ToList());

}



这个我正在使用的视图,想在更新时执行编辑(提交)按钮







@foreach(型号中的var项目)

{



FirstName @ Html.EditorFor(model => item.FirstName)
LastName @ Html.EditorFor(model => item.LastName)
Title @ Html.EditorFor(model => item.Title)
BirthDate @ Html.Ed itorFor(model => item.BirthDate)
地址 @ Html.EditorFor(model => item.Address)
City @ Html.EditorFor(model => item.City)
< input type =submit value =更新id =更新/>


< script type =text / javascript>

$(document).ready(function(){

$(。datepicker)。datepicker();

});













})

< / script>

}







之后我怎么能调用动作执行编辑

解决方案

(文件).ready(function(){


( .datepicker)。datepicker();

});













})

< / script>

}







之后我怎么能调用动作来执行编辑


您的编辑操作可以接收表单字段FormCollection并按如下方式访问它们:



 [HttpPost] 
public ActionResult EditEmployee(FormCollection order)
{
string FirstName = Convert.ToString(order [ FirstName]); // 访问此类表单项
}





最好的方法是使用强类型模型,这样你就可以得到如下代码:



< pre lang =c#> [HttpPost]
public ActionResult EditEmployee(订单)
{
string FirstName = Order.FirstName; // 访问此类表单项
}





为了获得强类型对象,您必须按如下方式更改编辑操作:

  public  ActionResult EditEmployee( int  id =  0 
{
返回查看( new 列表< order>(db.Employees.Where (o = > o.EmployeeID == id)。ToList()));
}
< / 订单 >


i'm taking a list to bind in view so i have the details when page loads in the same view i wanna perform edit on when i click on edit button


this is the action which i use to show the details when page loads
public ActionResult EditEmployee(int id = 0)
{
return View(db.Employees.Where(o => o.EmployeeID == id).ToList());
}

And this the view I'm using and wanna perform edit on update (submit)buttton



@foreach (var item in Model)
{

FirstName @Html.EditorFor(model => item.FirstName)
LastName @Html.EditorFor(model => item.LastName)
Title @Html.EditorFor(model => item.Title)
BirthDate@Html.EditorFor(model => item.BirthDate)
Address@Html.EditorFor(model => item.Address)
City@Html.EditorFor(model => item.City)
<input type="submit" value="Update" id="update" />

<script type="text/javascript">
$(document).ready(function () {
$(".datepicker").datepicker();
});






})
</script>
}



after this how can i call the action to perform the edit

解决方案

(document).ready(function () {


(".datepicker").datepicker();
});






})
</script>
}



after this how can i call the action to perform the edit


Your edit action can receive the form fields as "FormCollection" and access them as follows:

[HttpPost]
       public ActionResult EditEmployee(FormCollection order)
       {
           string FirstName = Convert.ToString(order["FirstName"]); //access your form items like this
}



The best way is to have strongly typed model such that you have have your code as follows:

[HttpPost]
       public ActionResult EditEmployee(Order order)
       {
           string FirstName = Order.FirstName; //access your form items like this
}



In order to have your strongly typed object you have to change your edit action as follows:

public ActionResult EditEmployee(int id = 0)
{
return View(new List<order>(db.Employees.Where(o => o.EmployeeID == id).ToList()));
}
</order>


这篇关于我想在MVC4中进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发语言最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆