Why asp.net model binder can not map a string values to a Object property that have the same name -


i have model class named server , have created new servertoedit viewmodel class, follow:-

public class servertoedit     {         public server server { get; set; }        [required]         public string ipaddress { get; set; }     } 

part of create view is:-

    model tms.viewmodels.servertoedit      @* partial view defines form fields appear when creating , editing entities *@      @html.antiforgerytoken()     <div class="editor-label">         @html.labelfor(model => model.server.customername)     </div>     <div class="editor-field">         @html.editorfor(model =>model.server.customername)         @html.validationmessagefor(model =>model.server.customername)     </div>     <div class="editor-label">        ip address     </div>     <div class="editor-field">         @html.editorfor(model => model.ipaddress)         @html.validationmessagefor(model => model.ipaddress)     </div>  ipaddress <div class="editor-label">     @html.labelfor(model =>model.server.iloip) </div> <div class="editor-field">     @html.editorfor(model =>model.server.iloip)     @html.validationmessagefor(model =>model.server.iloip) </div> 

the create actin method :-

[httppost]         [validateantiforgerytoken]         public actionresult create(server server, technologyip technologyip)         {            try             {                 if (modelstate.isvalid)             {                 repository.insertorupdateserver(server,technologyip);                 repository.save();                 return redirecttoaction("index");             } 

but model binder not able bind ipaddress field inside view technologyip.ipaddress property? technologyip model class :-

public class technologyip     {         public string ipaddress { get; set; }          public int id { get; set; }     } 

on first look, imagine it's because model passed view not same model asking back. change line:

public actionresult create(server server, technologyip technologyip) 

to

public actionresult create(servertoedit servertoedit) 

and line:

repository.insertorupdateserver(server,technologyip); 

to

repository.insertorupdateserver(servertoedit.server, servertoedit.technologyip); 

let know how on.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -