x666ep
x666ep

Reputation: 303

Dictionary<TEntity, string> and asp.net mvc how to?

I have:

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

it's all represents a nomenclature of goods in my application. I'am tryin create new Nomenclature in my app. I use NHibernate. I create controller and create action:

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

I need to foreach all NomenclatureAttrributes specified for any Nomenclature Type and create editors for all values and add all to 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>
}

I use Asp.net core web application (.NET Framework)

Upvotes: 0

Views: 149

Answers (1)

Miguel
Miguel

Reputation: 150

First Create ViewModel.

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

Second

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

Than fix your view

@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>
}

if you want to use Nomenclature as ViewModel you can create new Nomenclature on Get Method than pass to view in 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> 

Upvotes: 2

Related Questions