2

i'm using razor's listboxfor for the first time, but my Model is always null. after reading similar posts and tryouts it still won't work.

Person.cshtml

@model SampleApp.Web.ViewModel.PersonViewModel

@{
     ViewBag.Title = "Welcome";
}

<article>
   <p>
      Welcome to example page. 
   </p>

   <p>
     <div class="container">

 //Post data works as expected, controllers create method write to db successfully 
 @using (Html.BeginForm("Create", "Person", FormMethod.Post, new { enctype =   "multipart/form-data" }))
 {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Personen</legend>

        <div class="editor-label">
           @* @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Age)
            @Html.ValidationMessageFor(model => model.Age)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Surrname)
        </div>
</fielset>
 </div>

 <p>
    <input type="submit" value="Create" />
 </p>
}

//binding to Model fails, Model is null. Not be able to debug anything in    controller action, it stops when "loading" the page
@using (Html.BeginForm("GetListBoxData", "Person"))
{
   @Html.AntiForgeryToken()
   @Html.ValidationSummary(true)
   @Html.ListBoxFor(model => model.ListboxData, Model.ListboxData);
}

</div>

PersonController.cs

[AcceptVerbs(HttpVerbs.Get)]
    [ValidateAntiForgeryToken]
    public ActionResult GetListBoxData()
    {
        var data = new List<PersonViewModel>();
        data.Add(new PersonViewModel{Name = "Test", Surrname="testsurrname", Age=30});

        var viewModel = new PersonViewModel()
        {
            ListboxData = data.AsEnumerable().Select(s=> new SelectListItem{Value=s.Name ,Text = s.Surrname}),
        };

        return View(viewModel);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateAntiForgeryToken]
    public ActionResult GetListBoxData(PersonViewModel persondata)
    {
        //TODO: handle values from View
        return View(this);
    }

    [ValidateAntiForgeryToken]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create([Bind(Include = "Name, Surrname, Age")]  PersonViewModel persondata)
    {
        try
        {
            PersonService personDataProvider = new PersonService();
            personDataProvider.SavePerson(persondata);

            return new RedirectResult("SomewhereToGo");
        }
        catch (DataException ex)
        {
            //TODO: Log
        }

        return View(this);
    }

PersonViewModel

public class PersonViewModel
{
    public int PersonId{ get; set; }
    public int Age { get; set; }
    public string Name { get; set; }
    public string Surrname { get; set; }
    public IEnumerable<SelectListItem> ListboxData { get; set; }
}

writing values from editFor to db works as expected without code for listboxfor. after adding it to my html it should be filled from db on page loading, but I get a ReferenceNotSet Exception on page loading. Model.ListboxData is null, before GetListBoxData action is called.

Thanks a lot for your help!

4

1 回答 1

1
  1. 您的表单应通过 POST 而不是 GET 提交数据。而且,您不需要使用enctype = "multipart/form-data",除非您想通过 from 上传文件。
  2. 您的控制器中需要两个索引操作,一个用于将数据从控制器发送到视图,另一个用于在将表单提交(POST)到服务器时从视图中获取数据。
  3. 传递给 ListBox 的第一个参数(表达式)是指模型中的 Property,ListBox 中的选定项目将存储在该属性中,在本例中为 PersonId。

因此,您的视图应如下所示:

@model MVCApplication.Web.ViewModel.PersonViewModel 

@using (Html.BeginForm("Index", "Person"))
{
    @Html.ListBoxFor(model => model.PersonId, Model.ListBoxData)

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

然后,在您的控制器中,您将有两个这样的操作:

public ActionResult Index()
{
    var viewModel = new PersonViewModel()
    {
        ListboxData = data.Select(s => new SelectListItem { Value = s.PersonId.ToString(), Text = s.PersonId.ToString() }).AsEnumerable();
    };

    return View(viewModel);
}

[HttpPost]
public ActionResult Index(PersonViewModel viewModel)
{
  // code to save the data in the database or whatever you want to do with the data coming from the View
}

顺便说一句,在您的 ViewModel 中,您不必像那样定义 ListBoxData 属性,只需执行以下操作:

public class PersonViewModel
{
    public int PersonId{ get; set; }
    public IEnumerable<SelectListItem> ListBoxData { get; set; }
}
于 2013-08-25T00:05:26.673 回答