3

I've searched SO and couldn't find a specific answer to this problem. I have a model that has a dictionary object called Weekdays and I want to map that to a set of form fields where the key is the day and the value is if it was checked or not.

So the form looks like this:

Monday [ ]
Tuesday [ ]
Wednesday [ ]
Thursday [ ]
Friday [ ]

My model looks like this:

public class Event
{
  [Required(ErrorMessage="All must be checked")]    
  public Dictionary<string,bool> Weekdays { get; set; }
}

Controller:

namespace MvcApplication6.Controllers
{
  public class HomeController : Controller
  {
    public ActionResult Index()
    {
      Event e = new Event();

      e.Name = "awesome";

      e.Weekdays = new Dictionary<string, bool>()
      {
        {"Monday", false },
        {"Tuesday", true },
        {"Wednesday", true },
        {"Thursday", false },
        {"Friday", true },
      };     

      return View("Home", e);
    }


    [HttpPost]
    public ActionResult Index(Event e)
    {        
      var x = e.Weekdays["Monday"];     

      return View("Home", e);
    }

  }
}

My view:

@model MvcApplication6.Models.Event
@{
  Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<style>.formErrors{color:Red}</style>
  <title>Home</title>
</head>
<body>
  <div>
  @using (@Html.BeginForm("Index", "Home")) 
  {     
    // Weekdays   
    foreach (var i in Model.Weekdays)
    {       
      @i.Key @Html.EditorFor(model => model.Weekdays[i.Key]) <br />    
    }     

    <br /><br /><br />

    <input type="submit" value="submit me" />

  }

  </div>
</body>
</html>

Currently, I'm getting a runtime error on the foreach loop, "Object reference not set to an instance of an object."

What's the deal. Thanks. Also, is there a better way to do this altogether?

5
  • I guess you're getting this error when you post the form, not before that right? Commented Jul 25, 2011 at 16:14
  • This answer might provide the info you need: stackoverflow.com/questions/962189/… Commented Jul 25, 2011 at 16:24
  • and this too: haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx Commented Jul 25, 2011 at 16:25
  • Thanks. Yes, I'm getting this error when I post. Commented Jul 25, 2011 at 16:30
  • In regards to your 1st link, is it possible to do it without utilizing the Form Parameters? I want it to use model binding. For your second link, I've checked it out before, but I cannot get it to apply to what I'm trying to do. Some sample code that applies to my solution would be grateful. Thanks again. Commented Jul 25, 2011 at 17:14

1 Answer 1

1

Look at this post ASP.NET MVC Model Binder not working with a dictionary You can also write your own Model Binder for Dictionary, here's an example http://siphon9.net/loune/2010/11/dictionary-model-binder-in-asp-net-mvc2-and-mvc3/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.