c# - 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 represents nomenclature of goods in application. i'am tryin create new nomenclature in app. use nhibernate. create controller , 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 save changes. " +                                          "try again, , if problem persists see system administrator.");     }     return view(nomenclature); } 

i need foreach nomenclatureattrributes specified nomenclature type , create editors values , add model.attributes.

@model nomenclature @{ viewbag.title = "new nomenclature"; layout = "_layout"; } @using (html.beginform()) { @html.antiforgerytoken()  <div class="form-horizontal">     @html.validationsummary(true)      @foreach (var 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 should place 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)

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 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)             {                 (int = 0; < 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 want use nomenclature viewmodel can create new nomenclature on method pass 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>  

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -