2012-05-18 2 views
1

내가하고 싶은 일은 SelectList을 범주 리포지토리의 범주로 채 웁니다. 약간의 실물 모형 http://mockupbuilder.com/App/15379.어떻게 범주 목록으로 선택 목록 채우기

public class Category : AbstractEntity<Category> { 
    public String Title { get; set; } 
    public String Description { get; set; } 
} 

가 어떻게이 SelectList를 채울 않습니다

[HandleError] 
public class ProductController : Controller { 
    private IRepository<Product> exhibitions; 
    private IRepository<Category> categories; 
    private readonly Int32 PageSize = 18; 

    // ctor ... 

    [HttpPost] 
    public ActionResult Create(Product product, Guid categoryId) { 
     // validation ... 
     // properties setting ...    
     product.Category = categories.Get(categoryId); 
     return View(product); 
    } 

Category 클래스는 다음과 같이이다 :

는 내가 지금 가지고있는 것은 컨트롤러입니다? JSON을 사용하여 어떻게 만들 수 있습니까?

감사합니다.

답변

2

Listbag에 List를 넣고 aspx 코드를 사용하여 렌더링 할 수 있습니다. 뭔가 아래 같은이 같은

[HttpGet] 
public ActionResult Create() // your create page render action 
{ 
    Viewbag.CategoryList = categories.GetAll(); //put it into viewbag 

    return View(); 
} 

그리고보기 페이지에서 뭔가 :

<select name="categoryId"> <%-- use name attribute to bind action parameters and model --%> 
<%foreach (Category item in Viewbag.CategoryList) 
    { %> 
<option value="<%=item.Id %>"><%=item.Title %></option> 
<% } %> 
</select> 

당신이 JSON을 통해 카테고리를 채울합니다. 당신처럼 카테고리 컨트롤러에 새로운 액션을 작성해야합니다 : 호출하고 결과를 처리하기 위해

public class CategoryContrller : Controller{ 
    .... 

    [HttpGet] 
    public ActionResult GetAll() 
    { 
     var categories = categories.GetAll(); //put it into viewbag 

     return Json(categories, JsonRequestBehavior.AllowGet); 
    } 
} 

그리고 페이지의 JS 로직 사용 아약스에서.