asp.net mvc 4 -为什么我不能发布我的表单数据到我的HttpPost方法上c# MVC4

本文关键字:我的 数据 表单 HttpPost MVC4 方法 mvc net 为什么 布我的 不能 | 更新日期: 2023-09-27 17:50:25

我有一个麻烦,当我张贴我的表单数据到我的httppost方法。当我将数据发送给视图时,它具有值并且值显示得很好。但是当我想保存它(将它发送到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方法?

的附加信息,这是表单数据的值,已被选中,但没有发布到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

asp.net mvc 4 -为什么我不能发布我的表单数据到我的HttpPost方法上c# MVC4

我也面临着这种类型的问题,我的MVC 4网站没有在一个页面上发布。在我短暂的生命中,我花了很多天的时间试图用很多帖子和谷歌来解决这个问题。在尝试了这些帖子的许多答案都没有成功之后,我决定深入了解不允许我的表单发布的原因。我在互联网上看了看,最后决定安装小提琴(这个免费的工具埃里克劳伦斯允许捕捉客户端和服务器之间的任何交易)通过使用它来发布包含问题的页面,由于请求发送的大小和请求头的实际大小之间的差异,请求已经中止。通过继续搜索,它出现了,这是由于web配置文件的maxrequestlength默认情况下,不允许你发布一个页面有一些非常高的大小(是页面发送超过25张不同大小的图片到服务器与帖子)。所以我在我的web配置文件中重写了这个默认值,在系统中使用这行代码。web

    <system.web>
<httpRuntime executionTimeout="360" maxRequestLength="1000000" />
  .......
 .......
</system.web>

使用此代码,任何给定页面的帖子的最大长度为1G大小。通过这样做,并试图再次张贴我的页面,我已经成功张贴这之后。