使用相同的Action和Controller在MVC中添加/编辑 [英] Add/Edit in MVC using same Action and Controller
本文介绍了使用相同的Action和Controller在MVC中添加/编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们可以在一个Action和View中使用Entity Framework在MVC 4.0中添加和编辑记录吗?
请建议我。
谢谢,
问候,>
Ashish
Hi,
Can we add and edit record in MVC 4.0 using Entity Framework in a single Action and View?
Please suggests me.
Thank you,
Regards,
Ashish
推荐答案
假设您的UI上有两个按钮
suppose you have two buttons on your UI
<fieldset class="ui-grid-a">
<div class="ui-block-a">
<input type="submit" name="add" value="add" data-role="button" />
</div>
<div class="ui-block-b">
<input type="submit" name="edit" value="edit" data-role="button" />
</div>
</fieldset>
并在您的控制器中
and in your controller
[HttpPost]
public ActionResult accept(FormCollection frm)
{
// ...
if (frm[3].ToLower() == "add")
{
//code
}
else
{
//code
}
return View();
}
}
使用[HttpPost]属性并重载您的操作方法并在view使用submitt类型的按钮。
use "[HttpPost]" attribute and overload your action method and in view use a "button" of type "submitt".
这可能对你有用
[HttpGet]
公共ActionResult Foo(FooModel型号)
{
//做东西
}
[HttpPost]
公共ActionResult Foo(FooModel型号)
{
//做东西>
}
this might work for you
[HttpGet]
public ActionResult Foo(FooModel model)
{
// do stuff
}
[HttpPost]
public ActionResult Foo(FooModel model)
{
// do stuff
}
这篇关于使用相同的Action和Controller在MVC中添加/编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文