Dictionary< TEntity string>以及asp.net MVC如何

本文关键字:asp net MVC 如何 以及 TEntity string Dictionary | 更新日期: 2023-09-27 18:17:36

我有:

public class Nomenclature
{
    public virtual int NomenclatureId { get; set; }
    public virtual NomenclatureType NomenclatureType { get; set; }  
    public virtual IDictionary<NomenclatureAttribute, string> Attributes { get; set; }
}
public class NomenclatureType
{
    public virtual int NomenclatureTypeId { get; set; }
    public virtual string  Name { get; set; }
    public virtual ICollection<Nomenclature> Nomenclatures { get; set; }
    public virtual ICollection<NomenclatureAttribute> NomenclatureAttributes { get; set; }
    public NomenclatureType()
    {
        Nomenclatures = new HashSet<Nomenclature>();
        NomenclatureAttributes = new HashSet<NomenclatureAttribute>();
    }
}
public class NomenclatureAttribute
{
    public virtual int NomenclatureAttributeId { get; set; }
    public virtual string AttributeName { get; set; }
    public virtual string AttributeType { get; set; }
    public virtual NomenclatureType NomenclatureType { get; set; }
}

在我的应用程序中,它代表了商品的命名。我尝试在我的应用中创建新的命名法。我使用NHibernate。我创建控制器和创建动作:

[HttpGet]
public ActionResult Create(string nomenclatureType)
{
    if (nomenclatureType == null)
        return RedirectToAction("List", "Nomenclature");
    ViewData["NomenclatureAttributes"] =
        _repositoryNomenclatureType.Get(w => w.Name == nomenclatureType).NomenclatureAttributes.ToList();
    return View();
} 
[HttpPost]
public IActionResult Create(Nomenclature nomenclature)
{
    try
    {
        if (ModelState.IsValid)
        {
            _repositoryNomenclature.Create(nomenclature);
            return RedirectToAction("List", "Nomenclature");
        }
    }
    catch (Exception)
    {
        ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists see your system administrator.");
    }
    return View(nomenclature);
}

我需要查找为任何命名类型指定的所有nomenclatureattributes,并为所有值创建编辑器,并将它们添加到Model.Attributes。

@model Nomenclature
@{
ViewBag.Title = "New nomenclature";
Layout = "_Layout";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
    @Html.ValidationSummary(true)
    @foreach (var a in (List<NomenclatureAttribute>)ViewData["NomenclatureAttributes"])
    {
        <div class="form-group">
            <label class="control-label col-md-2">@a.AttributeName</label>
            <div class="col-md-10">
             **What i should place to this???**
            </div>
         </div>
    }
  <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

我使用Asp.net核心web应用程序。净框架)

Dictionary< TEntity string>以及asp.net MVC如何

First Create ViewModel.

    public class CreateNomenclatureViewModel
    {
//Add other properties if needed
     public NomenclatureType SelectedNomenclatureType { get; set; } 
     public  List<NomenclatureAttribute> Attributes { get; set;}
    }

第二

[HttpGet]
    public ActionResult Create(string nomenclatureType)
    {
        if (nomenclatureType == null)
            return RedirectToAction("List", "Nomenclature");
        var viewModel= new CreateMomenClatureViewModel
{
Attributes = _repositoryNomenclatureType.Get(w => w.Name == nomenclatureType).NomenclatureAttributes.ToList()
}
    return View(viewModel);
}

@model CreateNomenclatureViewModel
@{
ViewBag.Title = "New nomenclature";
Layout = "_Layout";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
    @Html.ValidationSummary(true)
     @if (Model != null && Model.Attributes != null)
            {
                for (int i = 0; i < Model.Attributes.Count; i++)
                {
                <div class="form-group">
            @Html.DisplayFor(modelItem => Model.Attributes [i].AttributeName) 
            @Html.TextBoxFor(modelItem => Model.Attributes [i].AttributeType ) 
         </div> 
    }
  <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

如果你想使用Nomenclature作为ViewModel,你可以在Get方法上创建新的Nomenclature,然后在razor视图中传递给view。

<div class="form-group">
                @Html.DisplayFor(modelItem => Model.Attributes.Keys.ElementAt(i).AttributeName) 
                @Html.TextBoxFor(modelItem => Model.Attributes.Keys.ElementAt(i).AttributeType ) 
             </div>