如何使用MVC3和C#在单个Razor视图上处理多个动作? [英] How to handle multiple actions on a single Razor view using MVC3 and c#?

查看:58
本文介绍了如何使用MVC3和C#在单个Razor视图上处理多个动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图在MVC3中的一个Razor视图表单上使用多项CRUD(Create,Read,Update,Delete)操作,因此我只能在控制器上声明我的操作名称
到不同的动作并在那里实现更多的实现.
我猜是这样的.

Hi,

I am trying to use muliple CRUD(Create,Read,Update,Delete) actions on one Razor view form in MVC3.And therefore i can only declare my action names on my controller
to different actions and have thier implementations there.
I assume something like this..

@using(Html.BeginForm("ControllerName","ModelName",[List of actions])

....
....




在我的Controller.cs上




On My Controller.cs

[HttpPost]
[ActionName("Create")]


我想我们也可以有自定义的html助手并执行此操作.但是我不确定如何进一步执行此操作.

有人可以帮我吗?我正在寻找更简单的方法.
我已经用谷歌搜索,但是大多数解决方案看起来都很复杂. :-)

谢谢,
Keerthi


I think we can also have custom html helpers and do this.But i am not sure how to proceed further with this.

Can any one help me with this?I am looking something more simpler way to do.
I have googled,but most of the solutions looks complex. :-)

Thanks,
Keerthi

推荐答案

没有现成的帮助程序可以为您构建它.由于这取决于您的业务需求,因此如何呈现操作.最简单的方法是使用多种形式.

在网页上放置许多表格没有问题.传统的ASP.NET通常不允许这样做,但是html标准对此没有任何禁止.因此,如果您希望所有操作都为后期操作,只需将所有操作添加为仅包含提交链接(或按钮)的简单表单即可.

There is no ready-made helper that builds this for you. Since it depends on your business needs how the actions will be presented. The simplest way is using many forms.

There is no problem placing many forms on a web page. Classic ASP.NET did not allow this in general, but the html standard has nothing against it. So if you want all your actions to be post actions, just add all as simple forms containing just a submit link (or button).

@using(Html.BeginForm("fooController", "DeleteAction", FormMethod.Post, new { id = model.id }))
{
   <input type="submit" value="Delete" />
}


等等...


and so on...


这有点令人困惑.

首先,您的以下代码是错误的

This is bit confusing.

First, your following code is wrong

@using(Html.BeginForm("ControllerName","ModelName",[List of actions])



BeginForm需要(ActionName,CntrollerName,Method,Parameters)而不是ModelName,

如果这是一个错字,那么在CRUD中,您只需要使用HTTP_POST的HTML中的Form,而Read和Delete是HTTP_GET的,因此您不需要这些形式.

对于创建和更新您需要表单",现在我不明白为什么您需要在同一视图上创建和更新表单?

假设您仍要执行此操作,则可以使用BeginForm或
使用多个表单
您可以创建用于创建和更新的局部视图,然后使用@ Html.Action(Controller,PartialView/ActionName)在主视图上呈现它们.如果执行此操作,则主视图上根本不需要任何表单.这样,您将在ASP.Net Webforms中使用这些部分视图,例如用户控件.



BeginForm takes (ActionName,CntrollerName,Method,Parameters) and not the ModelName,

if this is a typo then, in CRUD, you only need Form in HTML where you use HTTP_POST, and Read and Delete are HTTP_GET, so you do not need form for these.

For Create and Update you need form, now I do not understand why you need Create and Update Form on same view ?

Suppose you want to do this anyway, either you can use multiple forms using BeginForm or,

you can create Partial Views for Create and Update, and then render them on your main View using @Html.Action(Controller,PartialView/ActionName). if you do this you do not need any form at all on your main view. this way you will use those partial views like user controls in ASP.Net Webforms.


这篇关于如何使用MVC3和C#在单个Razor视图上处理多个动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆