假设我有 2 个控制器,TopicsController 和 PostsController。
对于每个控制器,我有几个视图(索引和详细信息)。
主题(索引) 视图继承 System. Web.Mvc.ViewPage>
Topic (Details) 视图继承 System.Web.Mvc.ViewPage ;
我使用 TopicFormViewModel 是因为我要随模型一起发送附加数据。
Post(详细信息) 视图仅继承 System.Web.Mvc.ViewPage< ;MessageBoard.Models.Post>
现在,我创建了一个部分视图(CreatePost.ascx),它(显然:p)用于创建新帖子。我希望能够在您在上面看到的所有视图上重复使用此控件。
更新
我尝试使用 <% Html.RenderPartial("New"); 渲染部分视图%>
来自我的 Topics/Index.aspx 视图,但这会导致异常
传递到字典中的模型项的类型为“System.Data.Linq.Table`1[MessageBoard.Models.Topic]”,但此字典需要类型为“MessageBoard.Models.Post”的模型项。< /p>
现在的问题是我的部分视图 (CreatePost.ascx) 接受 System.Web.Mvc.ViewUserControl
我不知道如何从我上面的所有观点中传递这一点。
我也不确定如何将 .ascx 值提交到某个 URL(即 /Topics/1/CreatePost),如何告诉提交按钮发布到该 URL?
预先感谢,
马尔科
Let's assume I have 2 Controllers, TopicsController and PostsController.
For each controller, I have a couple of views (Index & Details).
The Topic (Index) view inherits System.Web.Mvc.ViewPage<IEnumerable<MessageBoard.Models.Topic>>
The Topic (Details) view inherits System.Web.Mvc.ViewPage<MessageBoard.Models.TopicFormViewModel>
I'm using a TopicFormViewModel because I'm sending additional data along with the Model.
The Post (Details) view just inherits System.Web.Mvc.ViewPage<MessageBoard.Models.Post>
Now, I've created a partial view (CreatePost.ascx) which is (obviously :p) used to create a new Post. I want to be able to re-use this control on all of the views you see above.
Update
I've tried rendering the partial view using <% Html.RenderPartial("New"); %>
from my Topics/Index.aspx View, but that results in an exception
The model item passed into the dictionary is of type 'System.Data.Linq.Table`1[MessageBoard.Models.Topic]', but this dictionary requires a model item of type 'MessageBoard.Models.Post'.
Now the problem is that my partial view (CreatePost.ascx) accepts a System.Web.Mvc.ViewUserControl<MessageBoard.Models.Post>
and I'm not sure how to pass that from all my views above.
I'm also unsure know how to submit the .ascx values to a certain URL (i.e. /Topics/1/CreatePost), how do I tell the submit button to post to that URL?
Thanks in advance,
Marko
发布评论
评论(3)
你好,马科,
我不确定我理解“如何从我上面的所有视图中传递该内容”是什么意思,但我确信您不必从您的视图中传递 Post 实例。发生的事情是,从您的视图中,您将调用一个控制器操作来创建 Post 模型对象,然后将其绑定到 CreatePost.ascx 部分。
您有两个选择:
在 CreatePost.ascx 部分中,您可能正在使用表单。
如果您按照我展示的方式使用,您可以将第一个和第二个参数分别更改为将控制您的提交的操作和控制器的名称。
第二种选择是使用 jQuery。只需为您的表单设置一个 ID,然后
希望这会有所帮助!
PS 为了能够重用 CreatePost.ascx 部分,请将其放入共享视图文件夹(母版页所在的位置)中。
Ciao Marko,
I am not sure I understand what do you mean by "how to pass that from all my views above" but I am sure that you dont have to pass an instance of Post from your views. What is going on is that from your views you will invoke a controller action that creates the Post model object and then bind it to the CreatePost.ascx partial.
You have two options:
Inside your CreatePost.ascx partial you are probably using a form.
If you use in the way I am showing you can change the first and the second params respectively to the names of the Action and the Controller that would habndle your submit.
The second option is using jQuery. Simply set an ID for your form and then
Hope this helps!
P.S. To be able to reuse your CreatePost.ascx partial place it inside the shared view folder (where your master page is).
关于重用不在同一视图文件夹中的部分视图,请使用以下内容并传入所需的模型,或者您可以为其定义自定义路由。
In regards to reusing a partial view which is not in the same view folder, use the following and pass in the model required, alternatively you can define a custom route for it.
@Marko
另一种方法是在 PostController 中有一个如下所示的操作:
然后,无论您想要在哪里创建帖子,您都可以简单地调用此操作,该操作返回新帖子的强类型视图。
即使您通过网络进行补充 http 调用(IMO),该解决方案也具有将新帖子的初始化代码集中到单个位置的优点。
从视图“观点”来看,当用户按下“新帖子”按钮时,可以完成对操作的调用,然后将收到的标记注入模式对话框内或当前页面中您喜欢的位置。
希望有帮助!
@Marko
Another way would be to have in the PostController an Action like the following:
Then wherever you want to create a Post you can simply call this action that returns the strong-typed view for your new post.
Even if you have a supplementary http call over the network IMO this solution has the advantage to keep the initialization code of a new Post centralized to a single place.
From the View "point-of-view" the call to the action can be done when the user press the "New Post" button and then inject the received markup inside a modal dialog or in a place of your pleasure in the current page.
Hope it helps!