0

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

这是我的模型:

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 });
}
4

1 回答 1

1

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

我建议使用帮助程序,以确保您具有正确的格式。

@Html.CheckBoxFor(x => item.chkBillboard, new { @class="myCheckbox" })
于 2013-03-12T23:28:23.260 回答