View不会将数据传回MVC4中的Controller

时间:2015-03-26 14:10:59

标签: c# asp.net-mvc asp.net-mvc-4 model-view-controller

我在过去的3-4天里尝试找到答案。我做了很多搜索,并从已经提出基本相同问题的人那里得到了很多很棒的答案。但是,没有任何修复对我有用 这与工作有关,因此某些关键字/名称已更改。我已经完成了程序,Controller正在将列表数据传递给View,View正在填充Display / Textbox和隐藏字段。但是,返回到控制器的模型具有正确数量的行项目,只是一切都是0或null:

MODEL

namespace hereismynamespace
{
  public class CommissionCodeModel
  {
    public CommissionCodeModel()
    {
        CommCodes = new List<CommissionListingItem>();
    }

    public List<CommissionListingItem> CommCodes { get; set; }
  }
}

CONTROLLER

namespace hereismynamespace
{
  public class CommissionCodeController : Controller
  {
    [HttpGet]
    public ActionResult Manage()
    {
        var model = new CommissionCodeModel();
        var list = CommissionListing.Get(false);
        foreach (CommissionListingItem item in list)
        {
            model.CommCodes.Add(item);
        }
        return View(model);
    }

    [HttpPost]
    public ActionResult SaveData(List<CommissionListingItem> CommCodes)
    {
        //do stuff here

        return View();
    }
  }
}

查看

@using (Html.BeginForm("SaveData", "CommissionCode", FormMethod.Post))
{
<table id="commissionCodesPercentage" class="listing">
  <thead>
        <tr>
            <th>Comm ID</th>
            <th>Comm Code</th>
            <th>Percent</th>
        </tr>
    </thead>
    @for (var i = 0; i < Model.CommCodes.Count; i++)
    {
    <tr>
        <td>@Html.DisplayFor(model => Model.CommCodes[i].CommissionId)
            @Html.HiddenFor(model => Model.CommCodes[i].CommissionId)
        </td>
        <td>@Html.DisplayFor(model => Model.CommCodes[i].Description)
            @Html.HiddenFor(model => Model.CommCodes[i].Description)
        </td>
        <td>@Html.TextBoxFor(model => Model.CommCodes[i].Rate)
            @Html.HiddenFor(model => model.CommCodes[i].Rate)
        </td>
    </tr>
    }
    </table>
    <br /><br />   
    <button id="btnCommissionCodesSave" name="submit"type="submit"value="Save">Save</button>
    <button id="btnCommissionCodesCancel">Cancel</button>

委员会主席的定义

[Serializable]
public struct CommissionListingItem
{
 private System.Int32 _CommissionId;
 private System.String _Description;
 private System.Double _Rate;
 public System.Int32 CommissionId{get{return _CommissionId;}}
 public System.String Description{get{return _Description;}}
 public System.Double Rate{get{return _Rate;}}
 public CommissionListingItem(
  System.Int32 CommissionId, 
  System.String Description, 
  System.Double Rate) 
 {
  this._CommissionId = CommissionId;
  this._Description = Description;
  this._Rate = Rate;
 }

3 个答案:

答案 0 :(得分:3)

struct没有设置问题,因为View无法设置属性,因此所有内容都使用默认值发布:

[Serializable] 
public struct CommissionListingItem 
{ 
  private System.Int32 _CommissionId; 
  private System.String _Description; private
  System.Double _Rate; 
  public System.Int32 CommissionId
  {
   get
     {
      return _CommissionId;
     }
   set
     {
       _CommissionId = value;
     }
  } 
 public System.String Description
 {
   get
     {
       return _Description;
     }
   set
     {
       _Description = value;
     }
   } 
  public System.Double Rate
  {
   get{return _Rate;}
   set { _Rate = value;}
  } 
} 

答案 1 :(得分:2)

您的模型属于

类型
CommissionCodeModel

但是在你的帖子中你需要一个类型为

的模型
List<CommissionListingItem>

因此模型绑定器无法正确绑定到控制器模型。

答案 2 :(得分:1)

模型变量在lambdas中应该是小写的。

@for (var i = 0; i < Model.CommCodes.Count; i++)
{
<tr>
    <td>@Html.DisplayFor(model => model.CommCodes[i].CommissionId)
        @Html.HiddenFor(model => model.CommCodes[i].CommissionId)
        </td>
    <td>@Html.DisplayFor(model => model.CommCodes[i].Description)
        @Html.HiddenFor(model => model.CommCodes[i].Description)
        </td>
    <td>@Html.TextBoxFor(model => model.CommCodes[i].Rate)
        @Html.HiddenFor(model => model.CommCodes[i].Rate)
        </td>
</tr>
}