0

与えられたモデル:

    public class RegisterModel
    {
        <!-- some properties -->

        [Required]
        public Dictionary<DayOfWeek, bool> DaysAtWork { get; set; } 

        public RegisterModel()
        {
            DaysAtWork = new Dictionary<DayOfWeek, bool>
                         {
                             {DayOfWeek.Monday, true},
                             {DayOfWeek.Tuesday, true},
                             {DayOfWeek.Wednesday, true},
                             {DayOfWeek.Thursday, true},
                             {DayOfWeek.Friday, true}
                         };
        }
    }

コントローラ:

    public ActionResult Register()
    {
        var model = new RegisterModel();

        return View(model);
    }

    [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
    <!-- and at these point in model state property for DaysAtWork 
    Dictionary is null and the whole model is invalid - and property in model 
    parameter is also null -->
        if (ModelState.IsValid)
        {
            <!-- some logic here -->    
        }    
        return View(model);
    }

そして見る:

    <!-- some code -->
    @using (Html.BeginForm()) {
    @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
    <div>
        <fieldset>
            <legend>Account Information</legend> 

            <div>Days at work</div>

            <div>
                Monday @Html.CheckBoxFor(m => m.DaysAtWork[DayOfWeek.Monday])
            </div>
            <div>
                Tuesday @Html.CheckBoxFor(m => m.DaysAtWork[DayOfWeek.Tuesday])
            </div>
            <div>
                Wednesday @Html.CheckBoxFor(m => m.DaysAtWork[DayOfWeek.Wednesday])
            </div>
            <div>
                Thursday @Html.CheckBoxFor(m => m.DaysAtWork[DayOfWeek.Thursday])
            </div>
            <div>
                Friday @Html.CheckBoxFor(m => m.DaysAtWork[DayOfWeek.Friday])
            </div>


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

モデルで DaysAtWork プロパティが null に設定されているため、ユーザーを登録しようとしているときに問題が発生しました。Register() HttpGet メソッドで作成されたモデルを使用するように asp に指示するにはどうすればよいですか? 私は何を間違っていますか?

4

1 に答える 1

0

「週」またはそれに類似したモデルを作成する場合:

public Week(){
public int Id;
public int AccountId;
public bool Monday;
public bool ...;
public bool Friday;
}

これへの参照を Registermodel に追加します。

お役に立てれば!

于 2012-09-28T12:22:49.770 に答える