2013-07-12 2 views
0

У меня возникли проблемы с моделью просмотра, которая постоянно возвращает свойства null после публикации. Ниже мой код (это может быть проблема синтаксиса или два вызова класс или свойство такое же имя, как я видел в других постах, но я не мог видеть любой такой вопрос в коде):Добавлено ViewModel return null properties

VIEW МОДЕЛЬ:

public class ProductItem 
    { 
     public int ProductID { get; set; } 
     public string Code { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public string LongDescription { get; set; } 
     public int SupplierID { get; set; } 
     public string Dimensions { get; set; } 
     public double Price { get; set; } 
     public bool On_Sale { get; set; } 
     public double DiscountedPrice { get; set; } 
     public string Thumbnail { get; set; } 
     public string LargeImage { get; set; } 
     public string LargeImage2 { get; set; } 
     public string LargeImage3 { get; set; } 
     public string CrossRef { get; set; } 
     public byte Available { get; set; } 
     public double Weight { get; set; } 
     public byte Important { get; set; } 
     public virtual ICollection<ProductCategory> ProductCategories { get; set; } 

     // this is required on the page to allow products to be marked for deletion 
     public bool IsForDelete { get; set; } 
    } 

    public class ProductListViewModel 
    { 
     public IEnumerable<ProductItem> ProductItems { get; set; } 
     public IEnumerable<Category> CategoryItems { get; set; } 
    } 

CONTROLLER:

public ActionResult ProductList() 
     { 
      var productList = new ProductListViewModel(); 
      productList.ProductItems = productRepository.GetProductsWithDeleteOption().ToList(); 
      productList.CategoryItems = categoryRepository.GetCategories().ToList(); 
      return View(productList); 
     } 

     [HttpPost] 
     public ActionResult ProductList(ProductListViewModel productViewModel, FormCollection formCollection, string submit) 
     {    
      if (ModelState.IsValid) 
      { 
       // Check for submit action 
       if (submit == "Change Sort") 
       { 
        if (formCollection["Sortby"] == "ProductID") 
        { 
         OrderBy(productViewModel, formCollection, "m.ProductID"); 
        } 
        else if (formCollection["Sortby"] == "Code") 
        { 
         OrderBy(productViewModel, formCollection, "m.Code"); 
        } 
        else if (formCollection["Sortby"] == "Name") 
        { 
         OrderBy(productViewModel, formCollection, "m.Name"); 
        } 
        else if (formCollection["Sortby"] == "Price") 
        { 
         OrderBy(productViewModel, formCollection, "m.Price"); 
        } 
       } 
       else if (submit == "Delete all selected") 
       { 

       } 
       else if (submit == "Update All") 
       { 

       } 
       else if (submit == "Restrict Display") 
       { 

       } 
      } 
      return View(productViewModel); 
     } 

ВИД:

@model Admin.Models.ViewModels.ProductListViewModel 

@{ 
    ViewBag.Title = "View Products"; 
} 

@using (Html.BeginForm()) 
{ 
<h2>Product List as at @DateTime.Now.ToString("dd/MM/yyyy")</h2> 
<table> 
    <tr> 
     <td>Sort by:</td> 
     <td> 
      <select name="Sortby"> 
       <option value="ProductID">ProductID</option> 
       <option value="Code">Code</option> 
       <option value="Name">Name</option> 
       <option value="Price">Price</option> 
      </select> 
     </td> 
     <td> 
      <input type="radio" name="sortDirection" checked="checked" value="Asc" /> Ascending 
      <input type="radio" name="sortDirection" value="Desc" /> Descending 
     </td> 
     <td> 
      <input type="submit" name="submit" value="Change Sort" /> 
     </td> 
    </tr> 
    <tr> 
     <td>Display only : (category)</td> 
     <td>@Html.DropDownList("CategoryID", new SelectList(Model.CategoryItems, "CategoryID", "Name"), "All Categories")</td> 
     <td colspan="2"><input type="submit" name="submit" value="Restrict Display" /></td> 
    </tr> 
    <tr> 
     <td colspan="4"><br />Total Number of products: @Model.ProductItems.Count()</td> 
    </tr> 
</table> 
<table> 
    <tr> 
     <th> 
      Edit 
     </th> 
     <th> 
      Code 
     </th> 
     <th> 
      Name 
     </th> 
     <th> 
      Price 
     </th> 
     <th> 
      On_Sale 
     </th> 
     <th> 
      DiscountedPrice 
     </th> 
     <th> 
      Weight 
     </th> 
     <th> 
      Delete 
     </th> 
     <th></th> 
    </tr> 

@for (var i = 0; i < Model.ProductItems.ToList().Count; i++) 
{ 
    <tr> 
     <td> 
      @Html.HiddenFor(m => m.ProductItems.ToList()[i].ProductID)    
      @Html.ActionLink(Model.ProductItems.ToList()[i].ProductID.ToString(), "ProductEdit", new { id = Model.ProductItems.ToList()[i].ProductID }) 
     </td> 
     <td> 
      @Html.DisplayFor(m => m.ProductItems.ToList()[i].Code) 
     </td> 
     <td> 
      @Html.DisplayFor(m => m.ProductItems.ToList()[i].Name) 
     </td> 
     <td> 
      @Html.EditorFor(m => m.ProductItems.ToList()[i].Price) 
     </td> 
     <td> 
      @Html.CheckBoxFor(m => m.ProductItems.ToList()[i].On_Sale, new { id = "On_Sale_" + Model.ProductItems.ToList()[i].ProductID }) 
     </td> 
     <td> 
      @Html.EditorFor(m => m.ProductItems.ToList()[i].DiscountedPrice) 
     </td> 
     <td> 
      @Html.EditorFor(m => m.ProductItems.ToList()[i].Weight) 
     </td> 
     <td> 
      @Html.CheckBoxFor(m => m.ProductItems.ToList()[i].IsForDelete, new { id = Model.ProductItems.ToList()[i].ProductID }) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "ProductEdit", new { id = Model.ProductItems.ToList()[i].ProductID }) | 
      @Html.ActionLink("Details", "Details", new { id = Model.ProductItems.ToList()[i].ProductID }) | 
      @Html.ActionLink("Delete", "Delete", new { id = Model.ProductItems.ToList()[i].ProductID }) 
     </td> 
    </tr> 
} 

</table> 
<p> 
    <input name="submit" type="submit" value="Delete all selected" /> 
</p> 
<p> 
    <input name="submit" type="submit" value="Update All" /> 
</p> 

<p> 
    @Html.ActionLink("Add a new product", "ProductAdd") 
</p> 

} 

В пост действия, productViewMo del аргумент имеет свойства ProductItems и CategoryItems как null.

+0

поделиться своим кодом – Paritosh

+0

Могу ли я увидеть и ваш код HTML? – Raver0124

+0

добавил вид. –

ответ

1

Да будет аннулирована на пост обратно. У меня есть аналогичная таблица в одном из моих проектов, и это то, что я бы сделал, учитывая мою ситуацию.

Вы можете изменить модель представления, чтобы посмотреть, как это, то вы не должны делать так много преобразования в списки на ваш взгляд:

public class ProductListViewModel 
{ 
    public List<ProductItem> ProductItems { get; set; } 

    public List<Category> CategoryItems { get; set; } 
} 

Сейчас в просмотре он может выглядеть следующим образом (это это только часть его, то остальные вы можете просто пойти и добавить):

@for (int i = 0; i < Model.ProductItems.Count(); i++) 
{ 
    <tr> 
      <td> 
       @Html.DisplayFor(m => m.ProductItems[i].Name) 
       @Html.HiddenFor(m => m.ProductItems[i].Name) 
      </td> 
    </tr> 
    <tr> 
      <td> 
       @Html.CheckBoxFor(m => m.ProductItems[i].IsForDelete) 
      </td> 
    </tr> 
} 

Добавьте код и сделать некоторые отладки, чтобы увидеть, как значения возвращаются на отправить

Я надеюсь, что это помогает.

3

Хорошо, так что есть две проблемы.

  1. Я не понимаю, почему вы хотите опубликовать список из CategoryItems следует ожидать только выбранной категории, а не список

  2. Проблема с ProductItems это имя генерируется для <input> тегов. В настоящее время имя генерируется в name="[0].Price" в то время как он должен был name="ProductItems[0].Price"

Я изменил следующий код

@Html.EditorFor(m => m.ProductItems.ToList()[i].Price) 

в

@Html.EditorFor(m => m.ProductItems[i].Price) 

, и она работала.

Примечание: Я изменил IEnumerable<ProductItem> ProductItems к List<ProductItem> ProductItems в ProductListViewModel