2

I'm a Newbie in Asp.net mvc 4 development, I have been developing for years in webforms and now I'm starting to develop in mvc, and I'm having some problems

In my new project I need to use a MarkdownEditor, I have encapsulated it in SharedView to facility the reuse of it across my views, (like create a Web user control in Webforms)

The code of the shared view with markdown editor is:

@model string

<div id="markdown-editor">
    <div class="wmd-panel">
        <div id="wmd-button-bar"></div>
            @Html.TextArea("m_wmdinput", @Model, new { @class="wmd-input" })
        </div>
    <div id="wmd-preview" class="wmd-panel wmd-preview"></div>
</div>
<script>
     ....
     ....

And this is an example where I use it as Render.PartialView.

@model MyProject.Models.PostIt

@using (Html.BeginForm()) {

    <fieldset>
        <legend>PostIt</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.PublishingDate)
            @Html.EditorFor(model => model.PublishingDate)
            @Html.ValidationMessageFor(model => model.PublishingDate)
        </div>

        @Html.Partial("MarkdownEditor", @Model.Content)

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

The model code is :

public class PostIt
{
    public int ID { get; set; }
    public string Content { get; set; }
    public DateTime PublishingDate { get; set; }

    public PostIt()
    {
        ID = -1;
    }
}

And controller code:

public ActionResult Edit(int? id)
{
    PostIt postIt = new PostIt();

    if (id.HasValue)
    {
        postIt = new PostItBLL().GetByID(id.Value);
        if (postIt == null)
        {
            return HttpNotFound();
        }
    }
    return View(postIt);
 }

 [HttpPost]
 public ActionResult Edit(PostIt postit)
 {
     if (ModelState.IsValid)
     {
         Save(postit);
         return RedirectToAction("Index");
     }
     return View(postit);
 }

 public ActionResult Index()
 {
     return View(db.PostIt.ToList());
 }

 private void Save(PostIt postIt)
 {
     if (postIt.ID < 0)
     {
         new PostItBLL().Add(postIt);
     }
     else
     {
         new PostItBLL().Update(postIt);
     }
 }

The page loads correclty, but when I change som value and I click in save button I get the following error:

The model item passed into the dictionary is of type 'MyProject.Models.PostIt', but this dictionary requires a model item of type 'System.String'.

Any clue about the error I'm making?

Thanks for your help

Marc Cals
  • 2,963
  • 4
  • 30
  • 48

1 Answers1

1

The problem occurs because the Content property on the PostIt model is null. If you give Content a default value like string.Empty, the problem will not occur. You could do this:

@Html.Partial("MarkdownEditor", @Model.Content ?? string.Empty)

For an explanation, read the accepted answer for this question: ASP.NET MVC, strongly typed views, partial view parameters glitch

Community
  • 1
  • 1
Robin van der Knaap
  • 4,060
  • 2
  • 33
  • 48