简体   繁体   中英

model is null when trying to get data from view to controller

I have a module which send data from controller to view. it has multiple rows and it shows correctly as i wanted. now after making some changes by user i am trying again to save changes in database with the help of actionresult. but when i try to fetch values it say my model is empty/null but it's not ...i am not getting what is the issue...Thanks in advance...

Here is my model:

public class ManageAppsModel
{

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

Here is my view:

@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>
  }
}

Here is my actionresult:

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

Your checkboxes are named chk1 and chk2, but your field is called chkBillboard. When you post your value, it uses the names of the input fields to match up the field names in your model.

I suggest using a helper, which makes sure you have the correct format.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM