English 中文(简体)
ASP. NET MVC3:行动可以接受 mu模型参数?
原标题:ASP.NET MVC3: can Action accept mulitple model parameters?

例如:

class MyContoller
{
    [MyCustomAttribute]
    public ActionResult MyAction(ModelX fromRequest, ModelY fromSession, ModelZ fromCookie)
    {
       string productId = fromRequest.ProductId;
       string userId = fromSession.UserId;
       string cultureName = fromCookie.CultureName;
    }
}

理由:

我不想访问控制人员的请求、会议以及HttpContext,以及MVC3的默认想法,即把模型推向行动是非常巨大的。

I want the number of parameters of MyAction is easy to change. For example, if I add a new parameter, the system will try to look for values in Request, Session or Cookies by the name or type of the parameter (I think custom ModelBinders may be required for cookie values) and pass the filled model to my action. I don t have to write extra code.

习俗归属(例如,MyCustomAttribute)能否实现这一想法?

最佳回答

I am not sure I follow you about the custom attribute. What are you expecting the custom attribute to do?

是的,一种行动方法可以像你所希望的那样采用许多模型参数。 显然,在任何特定请求中,只有一人可以受到约束(因为只有一种模式才能有这种看法)。 无论首先发现谁会受到约束,而其他人则将无效。

因此,请你说:

public class ModelX {
    public string X {get;set;}
}

public class ModelY {
    public string Y {get;set;}
}

public class ModelZ {
    public string Z {get;set;}
}

And you have an action method like this:

public ActionResult DoIt(ModelX x, ModelY y, ModelZ z)
{
    return View();
}

在您的《意见》中:

@model ModelZ

@using(Html.BeginForm()) {
    @Html.TextBoxFor(x => x.Z)
    <input type="submit"/>
}

如果你将一些东西打入案文箱并提交,那么模型商将约束模型Z与你所输入的价值以及模型X和模型。 Y将是无效的。

如果你意味着一种行动方法能够同时约束多种模式,那么我就必须请你。 你们计划如何有不止一种模式? 你当然可以建立一个包含多种模式的总结模式,但只有一种观点。

问题回答

1. 创建综合企业 纳入<代码>ModelX、ModelYModelZ的观测。 然后,你可以举一例,介绍你的新格言,并将之传给你的控制器。

public class XYZViewModel
{
    public ModelX fromRequest { get; set; }
    public ModelY fromSession { get; set; }
    public ModelZ fromCookie { get; set; }
}


public class MyController
{
    [MyCustomAttribute]
    public ActionResult MyAction(XYZViewModel myModel)
    {
       string productId = myModel.fromRequest.ProductId;
       string userId = myModel.fromSession.UserId;
       string cultureName = myModel.fromCookie.CultureName;
    }
}

你们总是能够把多个参数用于你的控制者的行动,是的。 关键在于确保在申请中适当分类。 如果你重新使用表格,这意味着使用Html助手方法。

例如,请说你希望采取这样的行动:

public ActionResult Multiple(ModelA a, ModelB b)
{
    // ...
}

你们可以为每一种模式形成简单的部分观点:

@model MyProject.Models.ModelA
@Html.EditorForModel()

之后,在您的<>Multiple观点中,部分意见如下:

@{ using (Html.BeginForm("Multiple", "MyController", FormMethod.Get))
{
    @Html.Partial("A", new MyProject.Models.ModelA())
    @Html.Partial("B", new MyProject.Models.ModelB())
    <input type= submit  value= submit  />
} 

我在此确定了<>GET的方法,以便你能够轻易地看到MVC如何通过参数。 如果你提交表格,你就会看到MVC成功地使每个物体脱轨。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签