Bind on edit Asp.Net MVC

2019-08-14 22:55发布

问题:

on this question ModelBinding asp.net MVC List I was answered how to bind on a create action. But How I bind the same Movie class with it tags propertie on an edit action?

Because when I do this:

 public ActionResult Edit(string movieid)
    {
        if(!string.IsNullOrEmpty(movieid))
        {
            ViewBag.Edit = true;
            var movie= db.GetCollection().FindOne(new { Name = movieid});
            if(movieid== null)
                throw new HttpException(404, "Movie not found");
            return View(movie);
        }
        return RedirectToAction("Index","Home");
    }

On the View at the input text I get: System.Collections.Generic.List`1[System.String]

How I bind the list to an Input text, on the render of the view?

Thanks!

回答1:

you can render your list of tags (string) in one textbox using

@Html.TextBoxFor(x => x.Tags, new { @Value = string.Join(",", Model.Tags) })

and if you have implemented custom model binder as Darin suggested in his answer you referred to, it will bind again to Tags lit on server side



回答2:

Try returning

movie[0]

I believe you are returning a list of one movie instead of what the actual string value of the movie is.