1

私がやりたいことはSelectList、カテゴリ リポジトリからカテゴリを入力することです。ちょっとしたモックアップhttp://mockupbuilder.com/App/15379

私が今持っているのはコントローラーです:

[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);
    }

カテゴリ クラスは次のようになります。

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

に入力するにはどうすればよいSelectListですか? を使用してこれを作成するにはどうすればよいJSONですか?

ありがとう!

4

1 に答える 1

2

リストをビューバッグに入れて、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 ロジックで ajax を使用して呼び出し、結果を処理します。

于 2012-05-18T08:33:06.573 に答える