从表单调用@ Url.Action结果404错误

时间:2014-12-01 15:02:38

标签: asp.net-mvc get model-binding

我从事ASP.NET MVC应用程序。使用jQuery填充表,但现在并不重要。

问题在于: 当我使用表单标签进行调用时,出现错误404 Not Found。通过检查我确认模型已绑定好(它已填充)。

当我注释掉表单标签时,我可以调用方法,但是模型为null(就像没有绑定一样)。

以下是我使用的一些代码:

@model  PayrollModel

@*@using (Html.BeginForm("Index", "Request", FormMethod.Post, new { id = "frmPersonView" }))
{*@

    <table data-dataurl="@Url.Action("PayrollData", "Payroll")" id="tblData" class="table hover table-bordered table-striped"></table>
@*}*@

这是我的控制器方法:

[HttpGet]
public ActionResult PayrollData(PayrollModel model)
{
    //do something
}

编辑: 我发现了为什么会这样: 请求网址:

http:// localhost / CT.ISVDV.WebUI / Payroll / PayrollData?sEcho = 1&amp; iColumns = 5&amp; sColumns =&amp; iDisplayStart = 0&amp; iDisplayLength = 10&amp; mDataProp_0 = FIRSTNAME&amp; mDataProp_1 = LASTNAME&amp; mDataProp_2 = RIGHTGROUPNAME&amp; amp; ; mDataProp_3 = DISABILITYTYPENAME&安培; mDataProp_4 = AMOUNT&安培; iSortCol_0 = 0&安培; sSortDir_0 = ASC&安培; iSortingCols = 1&安培; bSortable_0 =真安培; bSortable_1 =真安培; bSortable_2 =真安培; bSortable_3 =真安培; bSortable_4 =真安培; SelectedTypes%5B3%5D = 4和; SelectedTypes %5B4%5D = 5&安培; SelectedTypes%5B5%5D = 6&安培; SelectedTypes%5B6%5D = 7&安培; SelectedTypes%5B7%5D = 8&安培; SelectedTypes%5B8%5D = 9&安培; SelectedTypes%5B9%5D = 10&安培; SelectedTypes%5B10 %5D = 11&安培; SelectedTypes%5B11%5D = 12&安培; SelectedTypes%5B12%5D = 13&安培; SelectedTypes%5B13%5D = 14&安培; SelectedTypes%5B14%5D = 15&安培; SelectedTypes%5B15%5D = 16&安培; SelectedTypes%5B16%5D = 17&安培; SelectedTypes%5B17%5D = 18&安培; SelectedTypes%5B18%5D = 19&安培; SelectedTypes%5B19%5D = 20&安培; SelectedTypes%5B20%5D = 21&安培; SelectedTypes%5B21%5D = 22&安培; SelectedTypes%5B22%5D = 2 3及SelectedTypes%5B23%5D = 24&安培; SelectedTypes%5B24%5D = 25&安培; SelectedTypes%5B25%5D = 26&安培; PayrollDate = 2014年1月12日&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B0%5D = 4和; PayrollSearch.RIGHTGROUPIDList.Value% 5B1%5D = 5&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B2%5D = 6&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B3%5D = 7和; PayrollSearch.RIGHTGROUPIDList.Value%5B4%5D = 8&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B5% 5D = 9&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B6%5D = 10&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B7%5D = 11&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B8%5D = 12&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B9%5D = 13安培; PayrollSearch.RIGHTGROUPIDList.Value%5B10%5D = 14&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B11%5D = 15&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B12%5D = 16&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B13%5D = 17&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B14%5D = 18&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B15%5D = 19&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B16%5D = 20&安培; PayrollSearc h.RIGHTGROUPIDList.Value%5B17%5D = 21&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B18%5D = 22&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B19%5D = 23&安​​培; PayrollSearch.RIGHTGROUPIDList.Value%5B20%5D = 24&安培; PayrollSearch。 RIGHTGROUPIDList.Value%5B21%5D = 25&安培; PayrollSearch.RIGHTGROUPIDList.Value%5B22%5D = 26&安培; tblData_length = 10&安培;

到目前为止,一切都是正确的,但随后我也传递了一些非模型属性的奇怪值。只有在我使用表单标签时才会发生这种情况,我需要这些。

DXScript = 1_157%2C1_89%2C1_88%2C17_28%2C17_2%2C1_149%2C1_86%2C1_141%2C1_96%2C17_7%2C1_139%2C1_98%2C1_97%2C17_8%2C1_155%2C1_125%2C1_156%2C1_118%2C17_9%2C1_148%2C1_147%2C1_132%2C17_27 %2C1_142%2C1_93%2C1_119%2C1_99%2C1_151%2C1_126%2C17_13%2C1_108%2C1_115%2C1_137%2C1_92%2C1_152%2C1_128%2C17_15%2C1_129%2C1_120%2C17_11%2C1_131%2C1_140%2C1_134%2C17_18%2C1_145%2C17_20%2C1_143%2C1_138 %2C1_146%2C1_150%2C17_23%2C17_26%2C1_95%2C5_5%2C5_4%2C4_11%2C4_10%2C4_6%2C4_7%2C4_9%2C17_14%2C4_12%2C1_107%2C1_110%2C4_13%2C4_14%2C1_106%2C1_124%2C17_12%2C1_144%2C7_48%2C1_91%2C7_50 %2C17_19%2C1_100%2C1_103%2C1_111%2C17_0%2C1_114%2C1_101%2C17_1%2C1_102%2C17_3%2C1_104%2C1_116%2C17_5%2C1_130%2C1_113%2C17_16%2C17_17%2C1_112%2C17_24%2C1_117%2C10_2%2C10_1%2C10_3%2C10_4%2C17_4 %2C9_15%2C9_12%2C9_10%2C17_21%2C9_14%2C9_11%2C9_13%2C8_10%2C8_17%2C8_24%2C8_26%2C8_9%2C8_12%2C8_13%2C8_18%2C17_22%2C8_21%2C8_23%2C8_22%2C8_16%2C8_19%2C8_20%2C8_14%2C8_15%2C8_25 %2C8_11%2C6_12%2C17_25&安培; DXCss =%2FCT.ISVDV.WebUI%2Fimg%2Ffavicon.ico%2 C%2FCT.ISVDV.WebUI%2FContent%2Fjvectormap%2Fjquery-jvectormap-1.2.2.css%2C%2FCT.ISVDV.WebUI%2FContent%2Fbootstrap.min.css%2C%2FCT.ISVDV.WebUI%2FContent%2Ffont- awesome.min.css%2C%2FCT.ISVDV.WebUI%2FContent%2Fionicons.min.css%2C%2FCT.ISVDV.WebUI%2FContent%2FiCheck%2Fall.css%2C%2FCT.ISVDV.WebUI%2FContent%2Fcolorpicker% 2Fbootstrap-colorpicker.min.css%2C%2FCT.ISVDV.WebUI%2FContent%2Ftimepicker%2Fbootstrap-timepicker.min.css%2C%2FCT.ISVDV.WebUI%2FContent%2Fmorris%2Fmorris.css%2C%2FCT.ISVDV。 WebUI中%2FContent%2Fdatepicker%2Fdatepicker3.css%2C%2FCT.ISVDV.WebUI%2FContent%2Fdaterangepicker%2Fdaterangepicker-bs3.css%2C%2FCT.ISVDV.WebUI%2FContent%2Fbootstrap-wysihtml5%2Fbootstrap3-wysihtml5.min.css% 2C%2FCT.ISVDV.WebUI%2FContent%2Fcss%2Fselect2.css%2C%2FCT.ISVDV.WebUI%2FContent%2Fselect2-bootstrap.css%2C%2FCT.ISVDV.WebUI%2FContent%2Fdatatables%2FdataTables.bootstrap.css% 2C%2FCT.ISVDV.WebUI%2FContent%2FAdminLTE.css%2C%2FCT.ISVDV.WebUI%2FContent%2FSite.css%2C%2FCT.ISVDV.WebUI%2FContent%2Fjquery.bootstra对touchspin%2Fjquery.bootstrap-touchspin.css%2C1_9%2C1_11%2C0_844%2C0_842%2C1_8%2C0_691%2C0_694%2C0_682%2C1_4%2C0_684%2C0_805%2C0_698%2C4_2%2C0_700%2C5_1%2C0_782%2C0_762%2C0_764%2C7_1% 2C7_0%2C1_1%2C0_657%2C0_869%2C0_871%2C0_777%2C8_2%2C0_779%2C8_0%2C0_794%2C6_2%2C0_796&安培; _ = 1417596051723

如何防止这种情况发生?

2 个答案:

答案 0 :(得分:0)

您的格式已关闭。尝试将单引号与双引号混合使用:

data-dataurl='@Url.Action("PayrollData", "Payroll")'

PayrollData需要PayrollModel模型并且您不提供任何模型。使用Url.Action发送整个模型没有合理的方法,所以请考虑只发送一个ID。

data-dataurl='@Url.Action("PayrollData", "Payroll", new { id = model.ID })'

答案 1 :(得分:0)

我认为你对它的运作方式非常困惑,尤其是模型绑定。

通常你会有这样的事情(假设我们只有工资单控制器)

[HttpGet]
public ActionResult Index()
{
 // here you would get your model that displays some data on that page
 PayrollIndexModel model = fetchModelData(); 
 return View(model)
}

及其观点

@model  PayrollIndexModel
@Model.PageTitle    
@using (Html.BeginForm("Request", "Payroll", FormMethod.Post, new { id = "frmPersonView" }))
{
     <table data-dataurl="@Url.Action("PayrollData", "Payroll")" id="tblData" class="table hover table-bordered table-striped"></table>         
}

然后,如果您单击表单上的保存,您可以调用下面的PayrollModel绑定到您的模型类,然后您可以使用它。

[HttpPost]
public ActionResult Request(PayrollModel model)
{
  // save stuff to database
}

让这个工作

@Url.Action("PayrollData", "Payroll")

您必须以这种方式创建操作,因为您通过GET方法请求数据而不是向服务器发送任何内容:

[HttpGet]
public ActionResult PayrollData()
{
 PayrollModel model = fetchModelData();
 return View(model)
}

但是,我看到你使用DataTables库,上次我使用它时我记得Datatable期待JSON对象。所以在这里你应该发送JSON。

[HttpGet]
public ActionResult PayrollData()
{
 PayrollModel model = fetchModelData(); 
 return Json(model)
}

转换为JSON的模型应该按照JQuery DataTable可以解析它(理解它)的方式进行格式化,例如:

{ "iTotalRecords":11,
"iTotalDisplayRecords":11,
"sEcho":1,
"aaData":[{"FirstName":"Dan","LastName":"Callahan","PhoneNumber":"(123) 555-5552","Age":35,"Birthday":"\/Date(218955600000)\/"},
{"FirstName":"Tom","LastName":"Gun","PhoneNumber":"(123) 555-5559","Age":59,"Birthday":"\/Date(-534538800000)\/"},
{"FirstName":"James","LastName":"Halk","PhoneNumber":"(123) 555-5554","Age":21,"Birthday":"\/Date(660027600000)\/"},
{"FirstName":"Jarold","LastName":"Interface","PhoneNumber":"(123) 555-5556","Age":39,"Birthday":"\/Date(86932800000)\/"},
{"FirstName":"Kevin","LastName":"Kentucky","PhoneNumber":"(123) 555-5551","Age":40,"Birthday":"\/Date(92638800000)\/"},
{"FirstName":"Justin","LastName":"Michaels","PhoneNumber":"(123) 555-5555","Age":27,"Birthday":"\/Date(470898000000)\/"},
{"FirstName":"Erich","LastName":"Milton","PhoneNumber":"(123) 555-5558","Age":54,"Birthday":"\/Date(-370724400000)\/"},
{"FirstName":"Mike","LastName":"Peterson","PhoneNumber":"(123) 555-5550","Age":24,"Birthday":"\/Date(568184400000)\/"},
{"FirstName":"Jason","LastName":"Ralph","PhoneNumber":"(123) 555-5557","Age":27,"Birthday":"\/Date(468306000000)\/"},
{"FirstName":"John","LastName":"Thompson21","PhoneNumber":"(123) 555-5545","Age":27,"Birthday":"\/Date(473317200000)\/"}
]}

为了让您的生活更轻松,请查看此库,它为您完成了大量繁琐的工作: https://github.com/ALMMa/datatables.mvc

祝你好运。