在ASP.NET MVC3,人们应该如何呈现多个型号支持多PartialViews? [英] In ASP.NET MVC3, how should one render multiple PartialViews backed by multiple Models?

查看:92
本文介绍了在ASP.NET MVC3,人们应该如何呈现多个型号支持多PartialViews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC3剃须刀,你如何创建多种形式的网页,让每一个表单的局部视图与它自己的模式呈现?

In MVC3 Razor, how do you create a page with multiple forms, so that each form is a partial view rendered with its own model?

我们一直在努力呼吁Html.RenderPartial(),在partialview名传递以及我们的模型,我们正在通过ViewBag访问的一个实例的形式多种多样,但每次我们尝试的方法似乎有严重的问题,所以我们必须有这个应该是怎样在一个理想的世界来操作根本性的误解。

We have been trying various forms of calling Html.RenderPartial(), passing in the partialview name as well as an instance of our models that we're accessing through the ViewBag, but every method we've tried seems to have serious issues, and so we must have a fundamental misunderstanding of how this is supposed to operate in an ideal world.

我们已经找到了一些类似主题的SO反应(<一个href=\"http://stackoverflow.com/questions/4419889/asp-net-mvc2-how-to-render-a-view-if-has-multiple-different-models\">like这个),建议包含到每个子模型的参考部分景色可能需要使用的,但我们找不到你怎么会真正使用这些子模型的数据,例如一个例子的超级模特当试图写EditorTemplates。

We have found some SO responses on similar topics (like this one) recommending a "super model" that contains references to each of the sub models our partial views might need to use, but we cannot find an example of how you would actually use those submodels' data, e.g. when trying to write EditorTemplates.

编辑:让我澄清我的问题有点。我们能够呈现partialview甚至一个动作(使用作为的RenderAction马克S.推荐)。然而,我们的主要问题是,当这些表单提交回,例如保存在partialview领域模型的状态,控制器似乎不知道我们在什么参考模型。我们应该如何调用到控制器?

Let me clarify my question a bit. We ARE able to render a partialview or even an action (using RenderAction as Mark S. recommended). However, our primary trouble is that when those forms submit back to, e.g. save the state of the Model in that partialview area, the Controller doesn't seem to know what model we're referencing. How should we be calling into the Controller?

我们已经尝试使这些局部视图的几种不同的方式。我们的第一个尝试是这样的:

We've tried a few different ways of rendering these partial views. Our first try was like this:

@using (Html.BeginForm("SaveAccountInfo", null, FormMethod.Post, new { id = "section1-form" }))
{
    @Html.EditorForModel("AccountInfo", (OurModels.AccountInfo) ViewBag.account_info)
}

该ViewBag.account_info包含这一个局部视图模型数据。

The ViewBag.account_info contains the Model data for this one partial view.

我们也试过:

@{Html.RenderPartial("FormAccountInfo", (OurModels.AccountInfo) ViewBag.account_info);}

和现在:

@{Html.RenderAction("FormAccountInfo", "Main", new { account_info = (OurModels.AccountInfo) ViewBag.account_info }); }

但无论哪一种,我们使用的页面,当表单被提交处理上呈现此一种形式的方法,我们似乎无法通过模型访问任何提交的数据的?我觉得我们正在做的事情很愚蠢,但我们是新来的MVC。

But no matter which method we use for rendering this one form on the page, when that form gets submitted for processing, we can't seem to access any of the submitted data via the Model? I feel like we're doing something really dumb, but we're new to MVC.

推荐答案

不要在抽象陷入了下来,从时间您呈现的页面到控制器方法被调用你的时间,HTML和HTTP正在努力只

Don't get bogged down in abstractions, from the time you render the page to the time the controller method is called you're working in HTML and HTTP only.

如果你想回发到不同的方法有很多方法可以做到这一点。你可以有你的页面上有多个HTML表单,或者您可以使用像jQuery来执行后。其中控制器动作拿起您的文章,并送回哪些数据是由HTML页面,其内容的形式来确定。 MVC的处理程序将拿起的请求,并尝试请求URL和内容绑定到与之相匹配的,可用的行动之一。

If you want to post back to a different method there's numerous ways to do so. You can have multiple HTML forms on your page, or you can use something like jQuery to perform a post. Which controller action picks up your post and what data is sent back is determined by the form on the HTML page and its contents. The MVC handlers will pick up the request, and attempt to bind the request URL and contents to one of the available actions that match it.

要回来后一模式,您需要将数据发送回一个合适的行动以这样的方式,该模型粘合剂可以读取它。这可能是JSON数据(如果你使用MVC 3)或名称 - 值对的形式。该控制器虽然一无所知正在请求数据,或者它是如何创建的页面。它甚至不知道该请求是否是由一个网页制作。

To "post back a model" you need to send the data back to a suitable action in such a way that the model-binder can read it. This could be JSON data (if you're using MVC 3) or a form with name-value pairs. The controller though knows nothing about the page that is requesting the data, or how it was created. It doesn't even know whether the request is being made by a page.

您可以通过一个普通的HTML页面上有一个表单设置张贴到你的MVC控制器之一的URL试试这个自己。只要表单域的名称相匹配的控制器操作的参数的属性的名称,MVC将绑定和调用操作。

You can try this for yourself by making a normal HTML page with a form on it that is set up to post to the URL of one of your MVC controllers. So long as the names of the form fields match the names of the properties on the argument of your controller action, MVC will bind it and call the action.

如果您有复杂的看法,你可能会发现,jQuery的岗位是去,与从页面数据收集一些客户端的方法和创建JSON对象送回来的路上。使用方法为您提供了什么样的数据被发送回很precise控制,而使用形式有限制(如重叠数据等)。

If you have complex views, you may well find that jQuery post is the way to go, with some client-side methods that gather data from the page and create JSON objects to send back. Using that method gives you very precise control over what data is sent back, whereas with forms you have limitations (such as overlapping data etc).

编辑 - 回应置评

下面是一个使用原始的HTML只显示一个非常简单的例子一个例子:

Here's an example that is using raw HTML only to show a very simple case:

Public Class HomeController

    'Other action methods...

    <HttpPost()>
    Function SayHello(myName As String) As ActionResult
        Return Content("Hello " & myName)
    End Function

End Class

和HTML表单(我plonked这对指数的看法,但它可以去任何地方绝对)

And HTML form (I plonked this on the Index view but it could go absolutely anywhere)

<form action="/Home/SayHello" method="post">
    <input type="text" name="myName" />
    <input type="submit" />
</form>

我已经设置在&LT的名称;输入&GT; 来匹配参数名称,这给MVC伸出援助之手。虽然被调回虽然只值1,我可以将其命名为别的东西。但是,如果我这样做:

I've set the name on the <input> to match the parameter name, which gives MVC a helping hand. While there is only 1 value being posted back though, I could name it something else. However if I did this:

<form action="/Home/SayHello" method="post">
    <input type="text" name="aName" />
    <input type="text" name="anotherName" />
    <input type="submit" />
</form>

...然后模型绑定不知道哪个值应使用,你会得到的值没有被正确设置。

...then the model binder wouldn't know which value it should use, and you'd get values not being set properly.

更改为这个但是:

<form action="/Home/SayHello" method="post">
    <input type="text" name="myName" />
    <input type="text" name="anotherName" />
    <input type="submit" />
</form>

...现在模型粘合剂是能够使用的名称正确地识别使用并且其值忽略。

... and the model binder is now able to use the name to correctly identify which value to use and which to ignore.

在强类型行动的情况:

Public Class HomeController

    <HttpPost()>
    Function SayHello(person As SimplePerson) As ActionResult
        Return Content("Hello " & person.FirstName)
    End Function

End Class

Public Class SimplePerson
    Public Property FirstName As String
    Public Property Surname As String
End Class

您需要有与属性名称对应格式的名称。我aName / anotherName形式会送回到正确的形状,但该模型粘结剂将不会设置任何属性,因为它不知道什么是什么。然而,这将工作:

You need to have form names that correspond with property names. My aName/anotherName form would send back the right shape, but the model binder wouldn't set any properties because it doesn't know what is what. This however would work:

<form action="/Home/SayHello" method="post">
    <input type="text" name="FirstName" />
    <input type="text" name="Surname" />
    <input type="submit" />
</form>

这篇关于在ASP.NET MVC3,人们应该如何呈现多个型号支持多PartialViews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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