PDA

View Full Version : پر کردن DropDownList



sorenaus
شنبه 22 آبان 1389, 15:55 عصر
با سلام

من در Post برای پر کردن DropDownList مشکل دارم، لطفا راهنمایی کنید


//

// GET: /Panel/Create

public ActionResult Create()
{
var group = from g in panel.tblGroups
select g;

ViewData["groupList"] = group;
return View();
}

//
// POST: /Panel/Create
[HttpPost]
public ActionResult Create([Bind (Exclude="id")]tblNew m)
{
try
{
panel.tblNews.InsertOnSubmit(m);
panel.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}

<%: Html.DropDownList("groupList", new SelectList((IEnumerable)ViewData["groupList"], "GroupID", "GroupName"))%>



با تشکر

sorenaus
یک شنبه 23 آبان 1389, 08:21 صبح
یعنی هیچ کس بلد نیست

از شما مهندسا بعیدها

z_bluestar
یک شنبه 23 آبان 1389, 15:05 عصر
من این شکلی این کار رو انجام می دم .
یک کلاس برای پر کردن DropDownList هام در برنامه ایجاد می کنم .


public IEnumerable<SelectListItem> GetCategories()
{
return GetCategories(-1);
}
public IEnumerable<SelectListItem> GetCategories(int SelectedCategoryID)
{
using (var rep = new Models.CategoryRepository())
{
var all = rep.Categories.ToArray();
return from c in all
select new SelectListItem()
{
Text = c.Name,
Value = c.CategoryID.ToString(),
Selected = SelectedCategoryID == c.CategoryID
};
}
}

بعد تو view برای پر کردن dropdownlist این کد رو می نویسم .

<div class="editor-field">
<%= Html.DropDownListFor(model => model.CategoryID,Html.DropDownSources().GetCategor ies()) %>
<%= Html.ValidationMessageFor(model => model.CategoryID) %>
</div>

Html.DropDownSources نام کلاس و Namespace است.