Pass a model to a partial view?
Your partial expects an instance of the MarkdownTextAreaModel
class. So do so, instead of passing an anonymous object which would throw anyways:
@Html.Partial("MarkdownTextArea", new MarkdownTextAreaModel { Name = "content" })
Now this being said a far better solution would be to adapt your view model, so that it contains a reference to MarkdownTextAreaModel
and use editor templates instead of partials in your views, just like so:
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; }
}
then of course readapt the controller serving this view so that it populates the MarkDown
of your view model:
public ActionResult Foo()
{
BlogContentModel model = .... fetch this model from somewhere (a repository?)
model.MarkDown = new MarkdownTextAreaModel
{
Name = "contect"
};
return View(model);
}
and then inside your main view simply:
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.Title)
@Html.TextBoxFor(m => m.Title)
@Html.EditorFor(x => x.MarkDown)
<input type="submit" value="Post" />
}
and then in order to follow standard conventions move your partial to ~/Views/YourControllerName/EditorTemplates/MarkdownTextAreaModel.cshtml
and now everything will magically come into place as it should.
bevacqua
Principal Software Engineer at Elastic, helping lead the Elastic Cloud UI team. ● Pre-IPO employee at Elastic, helping drive key initiatives in Elastic Cloud ● Authored 3 books on JavaScript/Node.js application architecture ● Organized first ever Node.js conference in Buenos Aires (NodeConf Argentina) ● Author dragula drag & drop library (20k stars) and prolific open source contributor ● Wrote my own MVC frameworks, clones of async, jQuery, Markdown parsers, and more (purely as a way of learning) ● Grew newsletter on JavaScript to 17k subscribers (Pony Foo Weekly) ● 40k karma on StackOverflow 😅 ● Avid reader Published author of several software development books: Mastering Modular JavaScript (O'Reilly, 2018), Practical Modern JavaScript (O'Reilly, 2017), and JavaScript Application Design (Manning, 2015). Nicolás has vast experience in tackling challenging technical problems, as well as in helping manage teams, driving technical innovation, collaborating across areas, and sharing his applied learning.
Updated on September 13, 2020Comments
-
bevacqua over 3 years
this is my partial:
@model RazorSharpBlog.Models.MarkdownTextAreaModel <div class="wmd-panel"> <div id="[email protected]"></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]" class="wmd-panel wmd-preview"></div> <div class="wmd-panel-separator"></div>
I'm trying to include it like this in my
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" /> }
these are the model classes:
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; } }
What am I doing wrong, how should I do this in order to make my partial reusable?
-
bevacqua over 12 yearsYay! I'm learning MVC as I'm coding, so this is great insight, thanks!
-
Akbari almost 9 yearsMy partial expects a long, how can I pass that?
@model long
and I'm trying something like@Html.Partial("p", new { m = Model.SWhCode })
.