use partial view in MVC 4 RRS feed

  • Question

  • Hi,

    for explain:

    i've a class contact and a contact can have one or more category (seller,customer,admin,...)

    I'd like to develop a partial view with 2 listbox. (one with all category and the other with the selected category)

    My issue is how can i pass all the categories in my home controller

    public ActionResult Edit(long id = 0)
                Contact contact = db.Contact.Find(id);
                if (contact == null)
                    return HttpNotFound();
                ViewBag.SocietyID = new SelectList(db.Society, "ID", "Name", contact.SocietyID);
                ViewBag.Categories= db.Category.ToList();
                return View(contact);

    How can i pass all the categories in my main view


    Finally what do i put in my partial view if i want display the 2 listbox.

    Simply the table is already a good start

    Thanks for your help

    • Moved by Mike Danes Tuesday, January 14, 2014 9:35 PM asp.net related
    Tuesday, January 14, 2014 8:31 PM


All replies

  • Please post ASP.NET related questions in the ASP.NET forums: http://forums.asp.net
    • Proposed as answer by Just Karl Tuesday, January 14, 2014 10:37 PM
    • Marked as answer by Just Karl Thursday, January 23, 2014 10:54 PM
    Tuesday, January 14, 2014 9:01 PM
  • Hello,

    Specifically, this should be asked in the ASP.Net MVC forum on forums.asp.net.


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C40686F746D61696C2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Tuesday, January 14, 2014 11:29 PM
  • This is an example of how i would do it

    In the controller
      public PartialViewResult BookById(int id)


           return PartialView("_BooksInLibary", db.Libary.Find(id).Books);


    In the Index

    <span class="btn btn-xs" onclick="AllBooks('@item.BookId')">@Html.DisplayFor(modelItem => item.Name)</span>

         function AllBooks(BookId) {


               url: '@Url.Action("BooksInLibary")',

               data: { id: BookId },

           success: function(data) {





     error: function (data){$('#Details').html('<h3>Error in retrieval</h3>');}



    Thursday, January 15, 2015 1:07 PM