如何获得在MVC控制器中的下拉值 [英] How to get drop down value in Controller in MVC
问题描述
使用递归函数结果使用字符串列表中我绑定下拉
我的下拉列表中有这样的价值
家
首页>>厨房
首页>> >>厨房ABC
I Bind dropdown using list of string using recursive function
My dropdown have value like
Home
Home>> Kitchen
Home>> Kitchen>> ABC
和我想同样的下拉值ABC数据库
and i want to Same dropdown value ABC in database
这是我的看法code
This is my View code
@{
ViewBag.Title = "Createnewproduct";
}
<h2>
Create new product</h2>
<div>
@using (Html.BeginForm("Createnewproduct", "ProductAdmin", FormMethod.Post, new { id = "sendFileForm", enctype = "multipart/form-data" }))
{
<table>
<tr>
<td>
Category
</td>
<td>
@Html.DropDownList("Test", new SelectList(ViewBag.ListOfDisciplines, Model))
</td>
</tr>
<tr>
<td>
Product Name
</td>
<td>
<input type="text" name="ProductName">
</td>
</tr>
<tr>
<td>
Product Description
</td>
<td>
<input type="text" name="ProductDescription">
</td>
</tr>
<tr>
<td>
Product long Description
</td>
<td>
<input type="text" name=" ProductlongDescription">
</td>
</tr>
<tr>
<td>
UPC
</td>
<td>
<input type="text" name="UPC">
</td>
</tr>
<tr>
<td>
SKU
</td>
<td>
<input type="text" name="SKU">
</td>
</tr>
<tr>
<td>
Stock
</td>
<td>
<input type="text" name="Stock">
</td>
</tr>
<tr>
<td>
Weight
</td>
<td>
<input type="text" name="Weight">
</td>
</tr>
<tr>
<td>
Height
</td>
<td>
<input type="text" name="Height">
</td>
</tr>
<tr>
<td>
Image URL
</td>
<td>
<input type="file" name="file" id="file" style="width: 100%;" id="Imageupload" />
</td>
</tr>
<tr>
<td rowspan="2">
<input type="submit" value="Save" />
</td>
</tr>
</table>
}
</div>
和我的控制器这样
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Createnewproduct(FormCollection form)
{
}
当我看到我的FormCollection价值在那里,我没有找到下拉值,所有其他值正常发现的FormCollection
when i see my FormCollection value there i did not find dropdown value , all other value properly find in FormCollection
请帮我在哪里,我在code做了错误
Please help me where i did the mistake in this code
public ActionResult Createnewproduct()
{
List<Category> categorylist = _Listofcategory();
var parentcate = categorylist.Where(c => c.ParentCategoryId == 1).ToList();
List<String> categoryList = new List<string>();
string prefix = ">>";
foreach (var item in parentcate)
{
prefix = item.CategoryName;
Setchild(prefix, item, categorylist, categoryList);
}
ViewBag.ListOfDisciplines = categoryList;
return View();
}
private void Setchild(string prefix, Category model, List<Category> listcategory, List<string> catStrings)
{
var childs = listcategory.Where(x => x.ParentCategoryId == model.CategoryId).ToList();
catStrings.Add(prefix);
if (childs.Count > 0)
{
foreach (var child in childs)
{
catStrings.Add(prefix + ">>" + child.CategoryName);
var subchild = listcategory.Where(c => c.ParentCategoryId == child.CategoryId).ToList();
if (subchild.Count > 0)
{
foreach (var subsubchild in subchild)
{
catStrings.Add(prefix + ">>" + child.CategoryName + ">>" + subsubchild.CategoryName);
var subsubsubchild = listcategory.Where(c => c.ParentCategoryId == subsubchild.CategoryId).ToList();
if (subsubsubchild.Count > 0)
{
foreach (var subsubsubsubchild in subsubsubchild)
{
catStrings.Add(prefix + ">>" + child.CategoryName + ">>" + subsubchild.CategoryName + ">>" + subsubsubsubchild.CategoryName);
}
}
}
}
}
}
}
这我的类别列表如何在视图模型中使用。
This my List of Category how to use in View model.
请让我知道
推荐答案
您可以使用下面提及的样本code为您的方案作为well.Here我已经使用视图模型。
You can use below mentioned sample code for your scenario as well.Here I have used ViewModel.
领域模型:
public class Product
{
public Product() { Id = Guid.NewGuid(); Created = DateTime.Now; }
public Guid Id { get; set; }
public string ProductName { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
}
public class ProductCategory
{
public int Id { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
视图模型:
public class ProductViewModel
{
public Guid Id { get; set; }
[Required(ErrorMessage = "required")]
public string ProductName { get; set; }
public int SelectedValue { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
[DisplayName("Product Category")]
public virtual ICollection<ProductCategory> ProductCategories { get; set; }
}
操作方法:
[HttpGet]
public ActionResult AddProduct() //generate view with categories for enter product data
{
//for get product categories from database
var prodcutCategories = Repository.GetAllProductCategories();
//for initialize viewmodel
var productViewModel = new ProductViewModel();
//assign values for viewmodel
productViewModel.ProductCategories = prodcutCategories;
//send viewmodel into UI (View)
return View("AddProduct", productViewModel);
}
[HttpPost]
public ActionResult AddProduct(ProductViewModel productViewModel) //save entered data
{
//get product category for selected drop down list value
var prodcutCategory = Repository.GetProductCategory(productViewModel.SelectedValue);
//for get all product categories
var prodcutCategories = Repository.GetAllProductCategories();
//for fill the drop down list when validation fails
productViewModel.ProductCategories = prodcutCategories;
//for initialize Product domain model
var productObj = new Product
{
ProductName = productViewModel.ProductName,
ProductCategory = prodcutCategory,
};
if (ModelState.IsValid) //check for any validation errors
{
//save recived data into database
Repository.AddProduct(productObj);
return RedirectToAction("AddProduct");
}
else
{
//when validation failed return viewmodel back to UI (View)
return View(productViewModel);
}
}
查看:
@model YourProject.ViewModels.ProductViewModel //set your viewmodel here
<div class="boxedForm">
@using (Html.BeginAbsoluteRouteForm("add", new { action = "AddProduct"},FormMethod.Post }))
{
<ul>
<li style="width: 370px">
@Html.LabelFor(m => m.ProductCategories)
@Html.DropDownListFor(m => m.SelectedValue,new SelectList(Model.ProductCategories, "Id",
"CategoryName"),"-Please select a category -")
@Html.ValidationMessageFor(m => m.ProductCategory.Id)
</li>
<li style="width: 370px">
@Html.CompleteEditorFor(m => m.ProductName, labelOverride: "Product Name")
@Html.ValidationMessageFor(m => m.ProductName)
</li>
</ul>
<div class="action">
<button class="actionButton" type="submit">
<span>Save</span></button>
</div>
}
</div>
最终输出
如果您对以上code片段的任何问题,PLZ让我知道。
我曾写过关于视图模型整篇文章,它在这里:的如何使用视图模型在ASP.NET MVC 。
If you have any problem about above code snippets,Plz let me know. I have written whole article about ViewModel,It's here :How to Use ViewModel with ASP.NET MVC .
我希望这会帮助你。
这篇关于如何获得在MVC控制器中的下拉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!