这是我的部分:
@model RazorSharpBlog.Models.MarkdownTextAreaModel
<div class="wmd-panel">
<div id="[email protected] /cdn-cgi/l/email-protection"></div>
@Html.TextAreaFor(m => m.Name, new { @id = "wmd-input-" + @Model.Name, @class = "wmd-input" })
</div>
<div class="wmd-panel-separator"></div>
<div id="[email protected] /cdn-cgi/l/email-protection" class="wmd-panel wmd-preview"></div>
<div class="wmd-panel-separator"></div>
我试图将它像这样包含在我的View
:
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.Title)
@Html.TextBoxFor(m => m.Title)
@Html.Partial("MarkdownTextArea", new { Name = "content" })
<input type="submit" value="Post" />
}
这些是模型类:
public class MarkdownTextAreaModel
{
[Required]
public string Name { get; set; }
}
public class BlogContentModel
{
[Required]
[Display(Name = "Post Title")]
public string Title { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Post Content")]
public string Content { get; set; }
}
我做错了什么,我应该如何做才能使我的部分可重用?
您的部分需要一个实例MarkdownTextAreaModel
班级。所以这样做,而不是传递一个无论如何都会抛出的匿名对象:
@Html.Partial("MarkdownTextArea", new MarkdownTextAreaModel { Name = "content" })
现在据说更好的解决方案是调整您的视图模型,以便它包含对MarkdownTextAreaModel
并在视图中使用编辑器模板而不是部分模板,就像这样:
public class BlogContentModel
{
[Required]
[Display(Name = "Post Title")]
public string Title { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Post Content")]
public string Content { get; set; }
public MarkdownTextAreaModel MarkDown { get; set; }
}
然后当然要重新调整服务于该视图的控制器,以便它填充MarkDown
你的视图模型:
public ActionResult Foo()
{
BlogContentModel model = .... fetch this model from somewhere (a repository?)
model.MarkDown = new MarkdownTextAreaModel
{
Name = "contect"
};
return View(model);
}
然后在你的主视图中简单地:
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.Title)
@Html.TextBoxFor(m => m.Title)
@Html.EditorFor(x => x.MarkDown)
<input type="submit" value="Post" />
}
然后为了遵循标准约定,将您的部分移至~/Views/YourControllerName/EditorTemplates/MarkdownTextAreaModel.cshtml
现在一切都会神奇地按其应有的方式就位。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)