为什么我不能 post 我的表单数据到 C# MVC 4 中的 HttpPost 方法?
Why I can't post my Form Data to my HttpPost method on C# MVC4?
当我 post 我的表单数据到我的 httppost 方法时遇到了麻烦。
当我将数据发送到视图时,它具有值并且值显示得很好。但是当我想保存它时(post 它到 httppost 方法),httpost 方法收到一个空对象。
这是我的 httpget 方法和 httppost 方法的代码
namespace DeviceBuilder.Controllers
{
public class WebServiceController : Controller
{
//
// GET: /WebService/
komponenClient client = new komponenClient();
debuDBDataContext debuDB = new debuDBDataContext();
public ActionResult ReqWebService(string devname, string compname)
{
ViewBag.message = "Choose your Device's Component";
var alljenis=client.loadJKbyDevCom(devname, compname);
IList<JeniskomponenModels> listjenis = new List<JeniskomponenModels>();
if (alljenis != null)
{
foreach (var datajenis in alljenis)
{
listjenis.Add(new JeniskomponenModels
{
id = datajenis.id,
device = datajenis.device,
component = datajenis.component,
name = datajenis.name,
brand = datajenis.brand,
information = datajenis.information,
price = datajenis.price,
stock = datajenis.stock,
});
}
}
return View(listjenis);
}
[HttpPost]
public ActionResult Build(IList<ComponentModels> model)
{
ViewBag.message = "Saved";
BuildRequest BR = new BuildRequest();
BuildRequestDetail BRD = new BuildRequestDetail();
int totalharga=0;
for (int i = 0; i < model.Count(); i++)
{
BR.UserName = User.Identity.Name;
BR.Status = "Saved";
BR.DateTime = DateTime.Now;
debuDB.BuildRequests.InsertOnSubmit(BR);
debuDB.SubmitChanges();
for (int j = 0; j < model.ElementAt(i).IlistJenis.Count(); j++)
{
if (model.ElementAt(i).SlistJenis.SelectedValue.ToString() == model.ElementAt(i).IlistJenis.ElementAt(j).name)
{
BRD.BuildID = BR.Id;
BRD.Device = model.ElementAt(i).IlistJenis.ElementAt(j).device;
BRD.JenisBrand = model.ElementAt(i).IlistJenis.ElementAt(j).brand;
BRD.JenisInformasi = model.ElementAt(i).IlistJenis.ElementAt(j).information;
BRD.Component = model.ElementAt(i).IlistJenis.ElementAt(j).component;
BRD.JenisName = model.ElementAt(i).IlistJenis.ElementAt(j).name;
BRD.JenisHarga = model.ElementAt(i).IlistJenis.ElementAt(j).price;
BRD.JenisStock = model.ElementAt(i).IlistJenis.ElementAt(j).stock;
totalharga += BRD.JenisHarga;
debuDB.BuildRequestDetails.InsertOnSubmit(BRD);
debuDB.SubmitChanges();
}
}
var update = (from buildrequest in debuDB.BuildRequests where buildrequest.Id == BRD.Id select new{
username=buildrequest.UserName, status=buildrequest.Status,
datetime=buildrequest.DateTime}).First();
BR.UserName = update.username;
BR.Status = update.status;
BR.DateTime = update.datetime;
BR.Total = totalharga;
debuDB.BuildRequests.InsertOnSubmit(BR);
debuDB.SubmitChanges();
}
return View(model);
}
[HttpGet]
public ActionResult Build(string devname)
{
ViewBag.message = "Choose your Device's Component";
ViewBag.Device = devname;
var all = client.loadJKbyDev(devname);
IList<JeniskomponenModels> listjenis = new List<JeniskomponenModels>();
IList<ComponentModels> listcomp = new List<ComponentModels>();
string compname = ""; int alreadycomp = 0, alreadyat = -1;
if (all != null)
{
for (var i = 0; i < all.Count(); i++)
{
compname = all.ElementAt(i).component;
for (var j = 0; j < listcomp.Count(); j++)
{
if (listcomp.ElementAt(j).Name == compname)
{
alreadycomp = 1; alreadyat = j;
//already at nya menuju ke 0 makanya naman nya snap dragon. buat ke yang ke tiga, pasti bisa
}
}
if (alreadycomp == 1)
{
listcomp.ElementAt(alreadyat).IlistJenis.Add(new JeniskomponenModels
{
brand = all.ElementAt(i).brand,
component = all.ElementAt(i).component,
device = all.ElementAt(i).device,
information = all.ElementAt(i).information,
name = all.ElementAt(i).name,
price = all.ElementAt(i).price,
stock = all.ElementAt(i).stock,
id = all.ElementAt(i).id,
});
listcomp.ElementAt(alreadyat).SlistJenis = new SelectList((IEnumerable<JeniskomponenModels>)listcomp.ElementAt(alreadyat).IlistJenis,"Id","Name");
}
else if (alreadycomp == 0)
{
listcomp.Add(new ComponentModels
{
DevName=all.ElementAt(i).device,
Name=all.ElementAt(i).component,
Supplier="e-Kelontong",
IlistJenis=new List<JeniskomponenModels>(){new JeniskomponenModels{
brand = all.ElementAt(i).brand,
component = all.ElementAt(i).component,
device = all.ElementAt(i).device,
information = all.ElementAt(i).information,
name = all.ElementAt(i).name,
price = all.ElementAt(i).price,
stock = all.ElementAt(i).stock,
id = all.ElementAt(i).id,
}},
});
listcomp.Last().SlistJenis = new SelectList((IEnumerable<JeniskomponenModels>)listcomp.Last().IlistJenis,"Id","Name");
}
}
}
return View(listcomp);
}
}
}
这是我的观点
@model IList<DeviceBuilder.Models.ComponentModels>
@{
ViewBag.Title = "Build";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.message</h2>
</hgroup>
<section class="contact">
<header>
<h3>@ViewBag.Device</h3>
</header>
@using(Html.BeginForm("Build","WebService",FormMethod.Post)){<table>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
<div>
@Html.LabelFor(M=>M.ElementAt(i).Name)
</div>
</td>
<td>
<div>
@Html.HiddenFor(M=>M.ElementAt(i).DevID)
@Html.HiddenFor(M=>M.ElementAt(i).DevName)
@Html.HiddenFor(M=>M.ElementAt(i).Id)
@Html.HiddenFor(M=>M.ElementAt(i).IlistJenis)
@Html.HiddenFor(M=>M.ElementAt(i).Supplier)
</div>
</td>
<td>
<div>
@Html.DropDownListFor(M=>M.ElementAt(i).SlistJenis,Model.ElementAt(i).SlistJenis,"Select")
</div>
</td>
</tr>
}
</table>
<input type="submit" value="Save">}</section>
有人可以帮我将我选择的值传回 httppost 方法吗??
有关其他信息,这是已选择但未 posted 到 httppost 方法的表单数据的值。我用 google chrome
搜索了它
-General
Remote Address:[::1]:62870
Request URL:http: //localhost:62870/WebService/Build
Request Method:POST
Status Code:500 Internal Server Error
-Response Headers
view source
Cache-Control:private
Content-Length:12139
Content-Type:text/html; charset=utf-8
Date:Fri, 22 May 2015 04:54:15 GMT
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpcS3VsaWFoXFNlbWVzdGVyIDRcUFJPS09GXERldmljZUJ1aWxkZXJcRGV2aWNlQnVpbGRlclxXZWJTZXJ2aWNlXEJ1aWxk?=
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:id-ID,id;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:315
Content-Type:application/x-www-form-urlencoded
Host:localhost:62870
Origin:http: //localhost:62870
Referer:http: //localhost:62870/WebService/Build?devname=Handphone
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
Form Data
view source
view URL encoded
DevID:0
DevName:Handphone
Id:0
IlistJenis:System.Collections.Generic.List`1[DeviceBuilder.Models.JeniskomponenModels]
Supplier:e-Kelontong
SlistJenis:3
DevID:0
DevName:Handphone
Id:0
IlistJenis:System.Collections.Generic.List`1[DeviceBuilder.Models.JeniskomponenModels]
Supplier:e-Kelontong
SlistJenis:2
Name
Build
1 requests ❘ 12.2 KB transferred ❘ Finish: 13 ms ❘ DOMContentLoaded: 42 ms ❘ Load: 36 ms
我的 MVC 4 网站也面临此类问题,但其中一个页面未 posting。在我短暂的生命中花了很多天试图用许多 post 和谷歌搜索来解决这个问题之后。在尝试了论文 post 的许多答案但没有成功之后,我决定深入了解不允许我的表格被 posted 的原因。我查看了互联网并最终决定安装 Fiddle(Eric Lawrence 的这个免费工具允许捕获客户端和服务器之间的任何事务),方法是将其用于 post 包含问题的页面,由于请求发送的大小与请求 headers 的实际大小之间存在差异,请求已中止。通过继续谷歌搜索,出现这是由于网络配置文件的 maxrequestlenght 默认情况下不允许您 post 具有一些非常大的页面(页面发送了最多 25 张不同的图片使用 post 设置为服务器的大小)。所以我用系统中的这行代码覆盖了我的网络配置文件中的这个默认值。网页
<system.web>
<httpRuntime executionTimeout="360" maxRequestLength="1000000" />
.......
.......
</system.web>
使用此代码,任何给定页面的 post 的最大长度为 1G 大小。通过这样做并再次尝试 post 我的页面,我在这之后 posting 成功了。
当我 post 我的表单数据到我的 httppost 方法时遇到了麻烦。 当我将数据发送到视图时,它具有值并且值显示得很好。但是当我想保存它时(post 它到 httppost 方法),httpost 方法收到一个空对象。
这是我的 httpget 方法和 httppost 方法的代码
namespace DeviceBuilder.Controllers
{
public class WebServiceController : Controller
{
//
// GET: /WebService/
komponenClient client = new komponenClient();
debuDBDataContext debuDB = new debuDBDataContext();
public ActionResult ReqWebService(string devname, string compname)
{
ViewBag.message = "Choose your Device's Component";
var alljenis=client.loadJKbyDevCom(devname, compname);
IList<JeniskomponenModels> listjenis = new List<JeniskomponenModels>();
if (alljenis != null)
{
foreach (var datajenis in alljenis)
{
listjenis.Add(new JeniskomponenModels
{
id = datajenis.id,
device = datajenis.device,
component = datajenis.component,
name = datajenis.name,
brand = datajenis.brand,
information = datajenis.information,
price = datajenis.price,
stock = datajenis.stock,
});
}
}
return View(listjenis);
}
[HttpPost]
public ActionResult Build(IList<ComponentModels> model)
{
ViewBag.message = "Saved";
BuildRequest BR = new BuildRequest();
BuildRequestDetail BRD = new BuildRequestDetail();
int totalharga=0;
for (int i = 0; i < model.Count(); i++)
{
BR.UserName = User.Identity.Name;
BR.Status = "Saved";
BR.DateTime = DateTime.Now;
debuDB.BuildRequests.InsertOnSubmit(BR);
debuDB.SubmitChanges();
for (int j = 0; j < model.ElementAt(i).IlistJenis.Count(); j++)
{
if (model.ElementAt(i).SlistJenis.SelectedValue.ToString() == model.ElementAt(i).IlistJenis.ElementAt(j).name)
{
BRD.BuildID = BR.Id;
BRD.Device = model.ElementAt(i).IlistJenis.ElementAt(j).device;
BRD.JenisBrand = model.ElementAt(i).IlistJenis.ElementAt(j).brand;
BRD.JenisInformasi = model.ElementAt(i).IlistJenis.ElementAt(j).information;
BRD.Component = model.ElementAt(i).IlistJenis.ElementAt(j).component;
BRD.JenisName = model.ElementAt(i).IlistJenis.ElementAt(j).name;
BRD.JenisHarga = model.ElementAt(i).IlistJenis.ElementAt(j).price;
BRD.JenisStock = model.ElementAt(i).IlistJenis.ElementAt(j).stock;
totalharga += BRD.JenisHarga;
debuDB.BuildRequestDetails.InsertOnSubmit(BRD);
debuDB.SubmitChanges();
}
}
var update = (from buildrequest in debuDB.BuildRequests where buildrequest.Id == BRD.Id select new{
username=buildrequest.UserName, status=buildrequest.Status,
datetime=buildrequest.DateTime}).First();
BR.UserName = update.username;
BR.Status = update.status;
BR.DateTime = update.datetime;
BR.Total = totalharga;
debuDB.BuildRequests.InsertOnSubmit(BR);
debuDB.SubmitChanges();
}
return View(model);
}
[HttpGet]
public ActionResult Build(string devname)
{
ViewBag.message = "Choose your Device's Component";
ViewBag.Device = devname;
var all = client.loadJKbyDev(devname);
IList<JeniskomponenModels> listjenis = new List<JeniskomponenModels>();
IList<ComponentModels> listcomp = new List<ComponentModels>();
string compname = ""; int alreadycomp = 0, alreadyat = -1;
if (all != null)
{
for (var i = 0; i < all.Count(); i++)
{
compname = all.ElementAt(i).component;
for (var j = 0; j < listcomp.Count(); j++)
{
if (listcomp.ElementAt(j).Name == compname)
{
alreadycomp = 1; alreadyat = j;
//already at nya menuju ke 0 makanya naman nya snap dragon. buat ke yang ke tiga, pasti bisa
}
}
if (alreadycomp == 1)
{
listcomp.ElementAt(alreadyat).IlistJenis.Add(new JeniskomponenModels
{
brand = all.ElementAt(i).brand,
component = all.ElementAt(i).component,
device = all.ElementAt(i).device,
information = all.ElementAt(i).information,
name = all.ElementAt(i).name,
price = all.ElementAt(i).price,
stock = all.ElementAt(i).stock,
id = all.ElementAt(i).id,
});
listcomp.ElementAt(alreadyat).SlistJenis = new SelectList((IEnumerable<JeniskomponenModels>)listcomp.ElementAt(alreadyat).IlistJenis,"Id","Name");
}
else if (alreadycomp == 0)
{
listcomp.Add(new ComponentModels
{
DevName=all.ElementAt(i).device,
Name=all.ElementAt(i).component,
Supplier="e-Kelontong",
IlistJenis=new List<JeniskomponenModels>(){new JeniskomponenModels{
brand = all.ElementAt(i).brand,
component = all.ElementAt(i).component,
device = all.ElementAt(i).device,
information = all.ElementAt(i).information,
name = all.ElementAt(i).name,
price = all.ElementAt(i).price,
stock = all.ElementAt(i).stock,
id = all.ElementAt(i).id,
}},
});
listcomp.Last().SlistJenis = new SelectList((IEnumerable<JeniskomponenModels>)listcomp.Last().IlistJenis,"Id","Name");
}
}
}
return View(listcomp);
}
}
}
这是我的观点
@model IList<DeviceBuilder.Models.ComponentModels>
@{
ViewBag.Title = "Build";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.message</h2>
</hgroup>
<section class="contact">
<header>
<h3>@ViewBag.Device</h3>
</header>
@using(Html.BeginForm("Build","WebService",FormMethod.Post)){<table>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
<div>
@Html.LabelFor(M=>M.ElementAt(i).Name)
</div>
</td>
<td>
<div>
@Html.HiddenFor(M=>M.ElementAt(i).DevID)
@Html.HiddenFor(M=>M.ElementAt(i).DevName)
@Html.HiddenFor(M=>M.ElementAt(i).Id)
@Html.HiddenFor(M=>M.ElementAt(i).IlistJenis)
@Html.HiddenFor(M=>M.ElementAt(i).Supplier)
</div>
</td>
<td>
<div>
@Html.DropDownListFor(M=>M.ElementAt(i).SlistJenis,Model.ElementAt(i).SlistJenis,"Select")
</div>
</td>
</tr>
}
</table>
<input type="submit" value="Save">}</section>
有人可以帮我将我选择的值传回 httppost 方法吗??
有关其他信息,这是已选择但未 posted 到 httppost 方法的表单数据的值。我用 google chrome
搜索了它-General
Remote Address:[::1]:62870
Request URL:http: //localhost:62870/WebService/Build
Request Method:POST
Status Code:500 Internal Server Error
-Response Headers
view source
Cache-Control:private
Content-Length:12139
Content-Type:text/html; charset=utf-8
Date:Fri, 22 May 2015 04:54:15 GMT
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpcS3VsaWFoXFNlbWVzdGVyIDRcUFJPS09GXERldmljZUJ1aWxkZXJcRGV2aWNlQnVpbGRlclxXZWJTZXJ2aWNlXEJ1aWxk?=
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:id-ID,id;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:315
Content-Type:application/x-www-form-urlencoded
Host:localhost:62870
Origin:http: //localhost:62870
Referer:http: //localhost:62870/WebService/Build?devname=Handphone
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
Form Data
view source
view URL encoded
DevID:0
DevName:Handphone
Id:0
IlistJenis:System.Collections.Generic.List`1[DeviceBuilder.Models.JeniskomponenModels]
Supplier:e-Kelontong
SlistJenis:3
DevID:0
DevName:Handphone
Id:0
IlistJenis:System.Collections.Generic.List`1[DeviceBuilder.Models.JeniskomponenModels]
Supplier:e-Kelontong
SlistJenis:2
Name
Build
1 requests ❘ 12.2 KB transferred ❘ Finish: 13 ms ❘ DOMContentLoaded: 42 ms ❘ Load: 36 ms
我的 MVC 4 网站也面临此类问题,但其中一个页面未 posting。在我短暂的生命中花了很多天试图用许多 post 和谷歌搜索来解决这个问题之后。在尝试了论文 post 的许多答案但没有成功之后,我决定深入了解不允许我的表格被 posted 的原因。我查看了互联网并最终决定安装 Fiddle(Eric Lawrence 的这个免费工具允许捕获客户端和服务器之间的任何事务),方法是将其用于 post 包含问题的页面,由于请求发送的大小与请求 headers 的实际大小之间存在差异,请求已中止。通过继续谷歌搜索,出现这是由于网络配置文件的 maxrequestlenght 默认情况下不允许您 post 具有一些非常大的页面(页面发送了最多 25 张不同的图片使用 post 设置为服务器的大小)。所以我用系统中的这行代码覆盖了我的网络配置文件中的这个默认值。网页
<system.web>
<httpRuntime executionTimeout="360" maxRequestLength="1000000" />
.......
.......
</system.web>
使用此代码,任何给定页面的 post 的最大长度为 1G 大小。通过这样做并再次尝试 post 我的页面,我在这之后 posting 成功了。