【问题标题】:Simple form post always sends null to controller简单的表单帖子总是向控制器发送 null
【发布时间】:2013-11-14 10:39:34
【问题描述】:

我不知道是什么原因,它是如此直接和简单,但奇怪的是,它不起作用。我总是收到 NULL 作为控制器中的模型。 代码如下:

型号

public class EnrolleePlaces
{
    [HiddenInput(DisplayValue=false)]
    public int id { get; set; }

    public string SpecialtyCode { get; set; }

    public string Specialty { get; set; }

    public int Places { get; set; }
}

控制器

public ViewResult EditPlaces(int id)
{
      return View(repo.EnrolleePlaces.FirstOrDefault(p => p.id == id));
}

[HttpPost]
public ActionResult NewPlaces(EnrolleePlaces places) // 'places' is ALWAYS null
{
       if (ModelState.IsValid)
       {
           repo.SaveEnrolleePlaces(places);
           return RedirectToAction("Settings");
       }
       return View("EditPlaces", places);
}

public ViewResult CreatePlaces()
{
      return View("EditPlaces", new EnrolleePlaces());
}

还有风景

@model Domain.Entities.EnrolleePlaces

@{
    Layout = null;
}

Edit: @Model.Specialty

@using (Ajax.BeginForm("NewPlaces", "Enrollee", new { area = "Admin" },
new AjaxOptions() { UpdateTargetId = "AdminContent" } ,
new { enctype = "multipart/form-data" }))
{
    @Html.EditorForModel()
    // here is input type="submit"
}

我的项目中有超过 15 个控制器,由相同的模式制作,但只有这个是 奇怪的

【问题讨论】:

  • 如果您在控制器中设置断点并检查Request.Form 集合,那里是否存在字段?
  • @AndersAbel 是的,它有正确的数据
  • 尝试将其更改为public ActionResult NewPlaces(string places),是否将参数传递给控制器​​?
  • @tomsullivan1989 它捕获了我的 int 字段
  • 试试public ActionResult NewPlaces(EnrolleePlaces area)

标签: c# asp.net asp.net-mvc-3 razor


【解决方案1】:

您是否尝试过更改您的操作方法接收的参数名称?

例如:

[HttpPost]
public ActionResult NewPlaces(EnrolleePlaces dd) // any name other than "places"
{
       if (ModelState.IsValid)
       {
           repo.SaveEnrolleePlaces(places);
           return RedirectToAction("Settings");
       }
       return View("EditPlaces", places);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 2012-03-18
    • 2015-11-22
    • 1970-01-01
    • 2019-08-06
    • 2019-08-14
    相关资源
    最近更新 更多