Bind on edit Asp.Net MVC

2019-08-14 23:21发布

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!

2条回答
爷、活的狠高调
2楼-- · 2019-08-14 23:33

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

查看更多
SAY GOODBYE
3楼-- · 2019-08-14 23:48

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.

查看更多
登录 后发表回答