尝试从视图到控制器获取数据时,model为null

时间:2013-03-12 22:10:26

标签: c# asp.net-mvc razor

我有一个模块,可以从控制器向视图发送数据。它有多行,它可以正确显示我想要的。现在用户做了一些更改后我再次尝试在actionresult的帮助下保存数据库中的更改。但是当我尝试获取值时,它说我的模型是空的/ null但它不是......我没有得到什么问题...提前感谢...

这是我的模特:

public class ManageAppsModel
{

  public string appname { get; set; }
  public int id { get; set; }
  public bool chkbillboard { get; set; }
}

以下是我的观点:

@model IEnumerable<Nd.Models.ManageAppsModel>

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <br />
  <h2 style="color:#80afdd;font-size:14px;">
    <strong> List of Existing Apps:</strong>
  </h2>
  <br />
  <div class="section-copy">
    <table>
    @foreach (var item in Model)
    {
      <tr>
      @if (Context.User.Identity.Name == "admin")
      {
        <td style="width:50px;">
        @if (@item.chkbillboard == true)
        {
          <input name ="chk1" class="myCheckbox" type="checkbox" value="@item.chkbillboard" checked="checked" />
        }
        else
        {
          <input name ="chk2" class="myCheckbox" id="chkbox" type="checkbox" value="@item.chkbillboard" onclick="return chkbox();" />
        }                                   
        </td>     
      }
        <td style="width:200px;">
          @item.appname
        </td> 
        <td style="width:50px;">
          @Html.ActionLink("Edit", "UpdateAPIForm", new { @id = item.id, appname = item.appname })
        </td>
      </tr>
      }
    </table>
  </div>
  <br/><br/>
  if (Context.User.Identity.Name == "admin")
  { 
    <div>
      <input type="submit" name="Upload" value="Upload new flash info message" />
    </div>
  }
}

这是我的行动结果:

[Authorize]
public ActionResult ManageApps(String username)
{
  var a = HttpContext.User.Identity.Name;
  var context = new ndCorp_SiteEntities();

  if (a == "admin")
  {
    var viewModel1 = from dc in context.DevContactInfoes
                     join dm in context.DevMarketplaceInfoes on dc.AppName equals dm.AppName
                     select new ManageAppsModel { appname = dc.AppName, id = dc.SNo, chkbillboard = dc.billboard.Value }
                     ;
    return View( viewModel1 );
  }
  else
  {
    var viewModel = from du in context.DevUserInfoes
                    join dc in context.DevContactInfoes on du.UserName equals dc.UserName
                    join dm in context.DevMarketplaceInfoes on dc.AppName equals dm.AppName
                    where du.UserName == a
                    select new ManageAppsModel { appname = dc.AppName, id = dc.SNo };
    return View(viewModel);
  }

}

[Authorize]
[HttpPost]
public ActionResult ManageApps(IEnumerable<ManageAppsModel> apps)
{
  var user = HttpContext.User.Identity.Name;
  var context = new ndCorp_SiteEntities();

  foreach (var ManageAppsModel in apps)
  {
    if (ManageAppsModel.chkbillboard == true)
    {
      Response.Write("hello");
    }
  }
  return RedirectToAction("ManageApps", new { username = user });
}

1 个答案:

答案 0 :(得分:1)

您的复选框名为chk1和chk2,但您的字段名为chkBillboard。发布值时,它使用输入字段的名称来匹配模型中的字段名称。

我建议使用帮助器,以确保您的格式正确。

@Html.CheckBoxFor(x => item.chkBillboard, new { @class="myCheckbox" })