2013-03-13 3 views
0

Я действительно получаю Model в Http Request. Но он не загружается в контроллер.Невозможно передать модель от View до Controller.

Модель

public class ConfigurableItemsModel 
{ 
    public IList<State> State { get; set; } 
    public IList<Country> Country { get; set; } 
    public IList<AccountType> AccountTypes { get; set; } 
    public IList<AddressSource> AddressSources { get; set; } 
    public IList<StopCode> StopCodes { get; set; } 

    public State state { get; set; } 
    public Country country { get; set; } 
    public AccountType accountType { get; set; } 
    public AddressSource addressSource { get; set; } 
    public StopCode stopCode { get; set; }  
} 

контроллер метод (Получить)

public ActionResult Edit(string ConfigName) 
{ 
    ConfigurableItemsClient client = new ConfigurableItemsClient(); 
    ConfigurableItemsModel configItemsModel = new ConfigurableItemsModel(); 
    List<ConfigurableItemsModel> configItemsModelList = new List<ConfigurableItemsModel>(); 

    switch (ConfigName) 
    { 
       case "Country": 
        List<Country> countryList = new List<Country>();     
        countryList = client.GetAllCountries().ToList(); 

        int count = countryList.Count; 

        for (int i = 0; i < count; i++) 
        { 
         configItemsModel.Country = new List<Country>(); 
         configItemsModel.Country = countryList; 
        } 
        configItemsModelList.Add(configItemsModel); 
        TempData["temporaryCountry"] = configItemsModel; 
        ViewBag.NoOfTimes = count; 
        ViewBag.ConfigItem = "Country"; 
        return View(configItemsModelList); 
        break; 
       case "State": 
        List<State> stateList = new List<State>(); 
        stateList = client.GetAllStates().ToList(); 
        configItemsModelList.Clear(); 

        for (int i = 0; i < stateList.Count; i++) 
        { 
         configItemsModel.State = new List<State>(); 
         configItemsModel.State = stateList; 
        } 
        configItemsModelList.Add(configItemsModel); 
        ViewBag.NoOfTimes = stateList.Count; 
        ViewBag.ConfigItem = "State"; 
        return View(configItemsModelList); 
        break; 
       case "Account Type": 
        List<AccountType> accountTypeList = new List<AccountType>(); 
        accountTypeList = client.GetAllAccountType().ToList(); 
        configItemsModelList.Clear(); 
        for (int i = 0; i < accountTypeList.Count; i++) 
        { 
         configItemsModel.AccountTypes = new List<AccountType>(); 
         configItemsModel.AccountTypes = accountTypeList; 
        } 
        configItemsModelList.Add(configItemsModel); 
        ViewBag.NoOfTimes = accountTypeList.Count; 
        ViewBag.ConfigItem = "AccountType"; 
        return View(configItemsModelList); 
        break; 
       case "Stop Code" : 
        List<StopCode> stopCodeList = new List<StopCode>(); 
        stopCodeList = client.GetAllStopCodes().ToList(); 
        configItemsModelList.Clear(); 
        for (int i = 0; i < stopCodeList.Count; i++) 
        { 
         configItemsModel.StopCodes = new List<StopCode>(); 
         configItemsModel.StopCodes = stopCodeList; 
        } 
        configItemsModelList.Add(configItemsModel); 
        ViewBag.NoOfTimes = stopCodeList.Count; 
        ViewBag.ConfigItem = "StopCode"; 
        return View(configItemsModelList); 
        break; 
       case "Address Source": 
        List<AddressSource> addressSourceList = new List<AddressSource>(); 
        addressSourceList = client.GetAllAddressSources().ToList(); 
        configItemsModelList.Clear(); 
        for (int i = 0; i < addressSourceList.Count; i++) 
        { 
         configItemsModel.AddressSources = new List<AddressSource>(); 
         configItemsModel.AddressSources = addressSourceList; 
        } 
        configItemsModelList.Add(configItemsModel); 
        ViewBag.NoOfTimes = addressSourceList.Count; 
        ViewBag.ConfigItem = "AddressSource"; 
        return View(configItemsModelList); 
        break; 
      } 

    return View(); 
} 

контроллер Метод (Post)

[HttpPost] 
public ActionResult Edit(ConfigurableItemsModel modelFromView, string EditViewButton) 
{ 
      ConfigurableItemsClient client = new ConfigurableItemsClient(); 
      switch (EditViewButton) 
      { 
       case "Add": 
        return View(); 
        break; 
       case "Edit": 
        return View(); 
        break; 
       case "Save": 
        //if(ViewBag.ConfigItem == "Country") 
        //{ 

         int i = 0; 
         Country NewCountry = new Country(); 
         NewCountry.CountryId = modelFromView.Country[i].CountryId; 
         NewCountry.CountryCode = modelFromView.Country[i].CountryCode; 
         NewCountry.CountryName = modelFromView.Country[i].CountryName; 
         NewCountry.WorkStationId = 1; 
         NewCountry.CreatedBy = 1; 
         NewCountry.CreatedOn = DateTime.Now; 
         NewCountry.ModifiedBy = 1; 
         NewCountry.ModifiedOn = DateTime.Now; 
         client.AddNewCountry(NewCountry); 
        //} 
        return View(modelFromView.Country); 
        break; 
      } 
      return View(); 
}  

View Page

@model IEnumerable<Models.ConfigurableItemsModel> 
@{ 
    ViewBag.Title = "Edit"; 
} 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Country</legend> 
     @if (ViewBag.ConfigItem == "Country") 
     { 
      <h2>Country</h2> 
      int k = 0; 


      <table> 
       <tr> 
        <th> 
         <label>Select</label> 
        </th> 
        <th> 
         @Html.DisplayNameFor(model => model.Country[k].CountryId) 
        </th> 
        <th> 
         @Html.DisplayNameFor(model => model.Country[k].CountryName) 
        </th> 
        <th> 
         @Html.DisplayNameFor(model => model.Country[k].CountryCode) 
        </th> 
       </tr> 

       @foreach (var item in Model) 
       { 
        for (int j = 0; j < item.Country.Count; j++) 
        { 

        <tr> 
         <th> 
          <input type="checkbox" name="chkCountry" /></th> 
         <th> 
          @Html.EditorFor(model => item.Country[j].CountryId) 
         </th> 
         <th> 
          @Html.EditorFor(model => item.Country[j].CountryName) 
         </th> 
         <th> 

          @Html.EditorFor(model => item.Country[j].CountryCode) 
         </th> 
        </tr> 

        } 

       } 

      </table> 
      <input type="submit" value="Save" name="EditViewButton" /> 

     } 
    </fieldset> 
} 

@if (ViewBag.ConfigItem == "State") 
{ 
    <h2>State</h2> 
    int k = 0; 
    <table> 
     <tr> 
      <th> 
       <label>Select</label> 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.State[k].StateId) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.State[k].CountryId) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.State[k].StateName) 
      </th> 
     </tr> 

     @foreach (var item in Model) 
     { 
      for (int i = 0; i < ViewBag.NoOfTimes; i++) 
      { 
      <tr> 
       <th> 
        <input type="checkbox" name="chkState" /></th> 
       <th> 
        @Html.DisplayFor(model => item.State[i].StateId) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.State[i].CountryId) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.State[i].StateName) 
       </th> 
      </tr> 
      } 
     } 
    </table>    

} 

@if (ViewBag.ConfigItem == "AddressSource") 
{ 
    <h2>Address Source</h2> 
    int k = 0; 
    <table> 
     <tr> 
      <th> 
       <label>Select</label> 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.AddressSources[k].Value) 

      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.AddressSources[k].ValueDescription) 

      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.AddressSources[k].DisplayOrder) 

      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.AddressSources[k].IsActive) 

      </th> 
     </tr> 

     @foreach (var item in Model) 
     { 
      for (int i = 0; i < ViewBag.NoOfTimes; i++) 
      { 
      <tr> 
       <th> 
        <input type="checkbox" name="chkAddressSource" /></th> 
       <th> 
        @Html.DisplayFor(model => item.AddressSources[i].Value) 
        @Html.HiddenFor(model => item.AddressSources[i].Value) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.AddressSources[i].ValueDescription) 
        @Html.HiddenFor(model => item.AddressSources[i].ValueDescription) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.AddressSources[i].DisplayOrder) 
        @Html.HiddenFor(model => item.AddressSources[i].DisplayOrder) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.AddressSources[i].IsActive) 
        @Html.HiddenFor(model => item.AddressSources[i].IsActive) 
       </th> 
      </tr> 
      } 
     } 
    </table>    

} 

@if (ViewBag.ConfigItem == "AccountType") 
{ 
    <h2>Account Type</h2> 
    int k = 0; 
    <table> 
     <tr> 
      <th> 
       <label>Select</label> 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.AccountTypes[k].Value) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.AccountTypes[k].ValueDescription) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.AccountTypes[k].DisplayOrder) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.AccountTypes[k].IsActive) 
      </th> 
     </tr> 

     @foreach (var item in Model) 
     { 
      for (int i = 0; i < ViewBag.NoOfTimes; i++) 
      { 
      <tr> 
       <th> 
        <input type="checkbox" name="chkAccountType" /></th> 
       <th> 
        @Html.DisplayFor(model => item.AccountTypes[i].Value) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.AccountTypes[i].ValueDescription) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.AccountTypes[i].DisplayOrder) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.AccountTypes[i].IsActive) 
       </th> 
      </tr> 
      } 
     } 
    </table>    

} 

@if (ViewBag.ConfigItem == "StopCode") 
{ 
    <h2>Stop Code</h2> 
    int k = 0; 
    <table> 
     <tr> 
      <th> 
       <label>Select</label> 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.StopCodes[k].Code) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.StopCodes[k].StopCodeName) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.StopCodes[k].StopCodeDescription) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.StopCodes[k].IsActive) 
      </th> 
     </tr> 

     @foreach (var item in Model) 
     { 
      for (int i = 0; i < ViewBag.NoOfTimes; i++) 
      { 
      <tr> 
       <th> 
        <input type="checkbox" name="chkStopCode" /></th> 
       <th> 
        @Html.DisplayFor(model => item.StopCodes[i].Code) 
        @Html.EditorFor(model => item.StopCodes[i].Code) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.StopCodes[i].StopCodeName) 
        @Html.EditorFor(model => item.StopCodes[i].StopCodeName) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.StopCodes[i].StopCodeDescription) 
        @Html.EditorFor(model => item.StopCodes[i].StopCodeDescription) 
       </th> 
       <th> 
        @Html.DisplayFor(model => item.StopCodes[i].IsActive) 
        @Html.EditorFor(model => item.StopCodes[i].IsActive) 
       </th> 
      </tr> 
      } 
     } 
    </table>    

} 

<table> 
    <tr> 
     <th>     
       <input type="submit" value="Delete" name="EditViewButton" />   
     </th> 
     <th>     
       <input type="submit" value="De-Activate" name="EditViewButton" />   
     </th> 
     <th> 
      @using (Html.BeginForm("Index", "ConfigurableItems", FormMethod.Get)) 
      { 
       <input type="submit" value="Cancel" /> 
      } 
     </th> 
     <th>     
       <input type="submit" value="Add" name="EditViewButton" />   
     </th> 
     <th>     
       <input type="submit" value="Edit" name="EditViewButton" />    
     </th> 
     <th>     
       <input type="submit" value="Save" name="EditViewButton" />   
     </th> 
    </tr> 
</table> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

Когда я нажимаю на Save кнопку настоящее время под стране, я могу видеть, что ценности, связанные с Страновые и EditViewButton становится принят в запросе, но не появляется в действии HttpPost Edit метода контроллера.

Я застрял с этой проблемой в течение двух дней, и google ничего не помогло. Цените свою помощь в этом.

ответ

0

Проблема заключается в том, что ваши поля ввода не соответствуют naming convention для привязки к списку. Причина этого в том, что вы используете внешний цикл foreach вместо цикла for.

Путь решить это, чтобы сделать ваш взгляд сильно типизированных индексированному коллекции (например, IList<T> или T[]):

@model IList<Models.ConfigurableItemsModel> 

, а затем заменить foreach цикл с for цикла:

@for (var i = 0; i < Model.Count; i++) 
{ 
    for (int j = 0; j < Model[i].Country.Count; j++) 
    { 
     <tr> 
      <th> 
       <input type="checkbox" name="chkCountry" /> 
      </th> 
      <th> 
       @Html.EditorFor(model => model[i].Country[j].CountryId) 
      </th> 
      <th> 
       @Html.EditorFor(model => model[i].Country[j].CountryName) 
      </th> 
      <th> 
       @Html.EditorFor(model => model[i].Country[j].CountryCode) 
      </th> 
     </tr> 
    } 
} 

Теперь, когда вы смотрите в сгенерированной разметке, а точнее на имя полей ввода, вы заметите, что они правильно названы. Например, вы увидите:

<input type="text" name="[1].Country[2].CountryId" value="123" /> 

вместо того, что вы в настоящее время есть и что неправильно:

<input type="text" name="Country[2].CountryId" value="123" /> 
+0

Спасибо. Я попробую это. –

+0

Я пробовал это, но все еще не могу попасть в контроллер. Модель ModelFromView всегда имеет значение null, но я могу видеть, что значения страны передаются по запросу. –

+0

Ваше действие контроллера POST должно принимать 'IList ' вместо 'ConfigurableItemsModel', потому что форма в вашем представлении содержит все эти элементы. Поэтому вы должны изменить подпись своего действия. –

0

Вы не можете размещать список значений с целью управления.

Вы должны принять ModelBinder и установить, чтобы Global.asax Application_Start()

protected void Application_Start() 
    { 
     System.Web.Mvc.ModelBinders.Binders.Add(typeof(ConfigurableItemsModel), new ConfigurableItemsModelBinder()); 
    } 

Теперь получить класс «ConfigurableItemsModelBinder» реализует «IModelBinder» и связать модель со значениями из поля зрения.

public class ConfigurableItemsModelBinder: IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
      List<Models.ConfigurableItemsModel> listModel=new List<Models.ConfigurableItemsModel>(); 
     for(int i=0;i<listModel.Count;i++) 
     { 
      //now get the value from view with the control name of view page. 
      string CountryId=Convert.ToString(bindingContext.ValueProvider.GetValue(Country[i].CountryId).AttemptedValue) //Country[i].CountryId it is the control name(dynamic) which your View HTML actually rendered. 
     // now put the CountryId into the model. 
      Country[i].CountryId=CountryId; 
     } 

    } 
} 

Контроллер: вам нужно передать список модели контроллера в пост

[HttpPost] 
public ActionResult Index(IEnumirable<ConfigurableItemsModel> model,string EditViewButton) 
    { 
     Return View(); 
    } 
Смежные вопросы