在操作结果中返回 EditorTemplate 作为 PartialView
我有一个与此类似的模型:
public class myModel
{
public ClassA ObjectA {get; set;}
public ClassB ObjectB {get; set;}
}
在我的主视图中,我有与此类似的标签:
<div id="section1">
<%=Html.EditorFor(m => m.ObjectA)%>
</div>
<div id="section2">
<%=Html.EditorFor(m => m.ObjectB)%>
</div>
ClassA 和 ClassB 都定义了编辑器模板。
我创建了一些 JavaScript 来进行 Ajax 调用来重新加载section1 div。我希望操作方法返回 EditorTemplates 文件夹中的 ObjectA、ClassA.ascx 的编辑器。
我的 Action 方法中有以下内容:
public ActionResult GetData(int input)
{
// Process input here and create modelData
return PartialView("ClassA", modelData);
}
这会出现错误,因为它找不到 ClassA 视图。
我的解决方案是在 Views 文件夹中创建一个名为“GetData”的 PartialView,并且我的返回呈现 GetData 视图。 GetData 视图只有一行代码:
<%=Html.RenderForModel()%>
这确实有效,但我想知道是否有一种方法可以让操作方法返回并编辑模板?
I have a model similar to this:
public class myModel
{
public ClassA ObjectA {get; set;}
public ClassB ObjectB {get; set;}
}
In my main view, I have tags similar to this:
<div id="section1">
<%=Html.EditorFor(m => m.ObjectA)%>
</div>
<div id="section2">
<%=Html.EditorFor(m => m.ObjectB)%>
</div>
ClassA and ClassB both have Editor templates defined.
I created some JavaScript that makes an Ajax call to reload the section1 div. I want the action method to return the editor for ObjectA, ClassA.ascx that is in the EditorTemplates folder.
I have the following in my Action method:
public ActionResult GetData(int input)
{
// Process input here and create modelData
return PartialView("ClassA", modelData);
}
This gives an error because it cannot find the ClassA view.
My solution has been to create a PartialView in the Views folder called "GetData" and my return renders the GetData view. The GetData view has only one line of code:
<%=Html.RenderForModel()%>
This does work, but I was wondering if there was a way for an action method to return and editor template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
礼品包装的奖励点:
让控制器(称为 MyController)继承自 CustomControllerBase,然后:
代码将查找“~/Views/MyController/EditorTemplates/MyViewModel.ascx”。
Bonus points for gift wrapping:
Have the controller (called, say, MyController) inherit from CustomControllerBase, and then:
The code will be looking for "~/Views/MyController/EditorTemplates/MyViewModel.ascx".
这对我有用(mvc 4)
this worked for me (mvc 4)