为什么我的操作参数为null?

时间:2016-11-14 23:22:47

标签: c# asp.net asp.net-mvc entity-framework

我正在尝试使用带有C#的ASP MVC 5创建一个页面,其中我有一些控件来输入搜索参数和一个显示结果的表。

问题是应该允许用户编辑表格中两个字段的文本并保存更改。

为了实现这一点,我的视图接收一个模型,其中有两个对象作为属性,一个用于搜索参数,一个用于结果列表,如下所示:

public class SEARCH_PAGE
{
    public List<VIEW_DATA_APR> table{ get; set; }
    public SEARCH parameters{ get; set; }
}

然后我的控制器是这样的:

public class CargaAPRController : Controller
{
    private datasource db = new datasource();
    // GET: CargaAPR
    public ActionResult Index()
    {
        try
        {
            List<SelectListItem> items = new SelectList(db.COMPANY, "IDCOMPANY", "COMPANYNAME").ToList();
            items.Insert(0, (new SelectListItem { Text = "ALL", Value = "0" }));
            ViewData["IDCOMPANY"] = items;
            var letters = (from c in db.LETTERS
                             select new VIEW_DATA_APR
                             {
                                 company = c.COMPANY.COMPANYNAME,
                                 idLetter = c.IDLETTER,
                                 nic = "not found",
                                 client = "not found",
                                 energy = 0,
                                 money = 0,
                                 period = "",
                                 letterCode = c.LETTERCODE
                             });

            SEARCH_PAGE sp = new SEARCH_PAGE();

            sp.table= letters.ToList();
            sp.parameters= new SEARCH();

            return View(sp);
        }
        catch (Exception ex)
        {
            return RedirectToAction("Error", new RouteValueDictionary(new { controller = "Users", action = "Error", mensaje = "Error: " + ex.Message }));
        }

    }

    [HttpPost]
    public ActionResult Index(SEARCH_PAGE model)
    {
        try
        {
            List<SelectListItem> items = new SelectList(db.COMPANY, "IDCOMPANY", "COMPANYNAME").ToList();
            items.Insert(0, (new SelectListItem { Text = "ALL", Value = "0" }));
            ViewData["IDCOMPANY"] = items;

            decimal company = Decimal.Parse(model.parameters.company);
            var letters= (from c in db.LETTERS
                             where (company== 0 ? c.IDCOMPANY: company) == c.IDCOMPANY
                             select new VIEW_DATA_APR
                             {
                                 company= c.COMPANY.COMPANYNAME,
                                 idLetter= c.IDLETTER,
                                 nic = "not found",
                                 client = "not found",
                                 energy = 0,
                                 money = 0,
                                 period = "",
                                 letterCode = c.LETTERCODE
                             });

            SEARCH_PAGE sp = new SEARCH_PAGE();

            sp.table= letters.ToList();
            sp.parameters = model.parameters;

            return View(sp);
        }
        catch (Exception ex)
        {
            return RedirectToAction("Error", new RouteValueDictionary(new { controller = "Users", action = "Error", mensaje = "Error: " + ex.Message }));
        }

    }

    [HttpPost]
    public ActionResult Save(SEARCH_PAGE model_search_page )
    {
        return View();
    }
}

我的观点是:

@using datasource.Models
@model SEARCH_PAGE
@{
ViewBag.Title = "Load APR";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@Styles.Render("~/Content/energetica.css")
<meta name="viewport" content="width=device-width" />
<title>Letters</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<div class="container-fluid">
    <div class="col-md-1">
</div>
<div class="col-md-10">
    <div class="panel panel-default">
        <!-- Default panel contents -->
        <div class="panel-heading">
            <div class="row">
                <div class="col-md-1">
                    <a href="@Url.Action("Index", "LoadFiles")" class="elements">
                        <img class="img img-responsive" style="width:25px; height:26px;padding:1px" src="~/img/BackIcon.png">
                    </a>
                </div>
                <div class="col-md-11 text-left" style="padding:1px;">
                    LOAD APR
                </div>
            </div>
        </div>
    </div>
    @using (Html.BeginForm("Index","LoadAPR", FormMethod.Post, null))
    {
        <table style="width:100%">
            <tr>
                <td style="width:10%">
                    <b>COMPANY: </b>
                </td>
                <td style="width:20%">
                    @Html.DropDownListFor(m => m.parameters.company, (IEnumerable<SelectListItem>)ViewData["IDCOMPANY"], new { htmlAttributes = new { @class = "form-control" } })
                </td>
                <td style="width:10%">
                    <b>PERIOD: </b>
                </td>
                <td style="width:20%">
                    @Html.EditorFor(m => m.parameters.period)
                </td>
                <td style="width:20%; text-align:right">
                    <input  type="submit" name="SEARCH" value="SEARCH" />
                </td>
            </tr>
            <tr>
                <td style="width:10%">
                    <b>CLIENT: </b>
                </td>
                <td style="width:20%">
                    @Html.EditorFor(m => m.parameters.client)
                </td>
                <td style="width:10%">
                    <b>NIC: </b>
                </td>
                <td style="width:20%">
                    @Html.EditorFor(m => m.parameters.nic)
                </td>
            </tr>
        </table>
    }
    <br />
    @using (Html.BeginForm("Save", "LoadAPR", FormMethod.Post, null))
    {
        <div style="overflow-y: scroll; max-height: 300px">
            <table style="width:100%">
                <tr>
                    <th>
                        @Html.Label("LBDIS", "Company")
                    </th>
                    <th>
                        @Html.Label("LBNLETTER", "Letter Code")
                    </th>
                    <th>
                        @Html.Label("LBNIC", "NIC")
                    </th>
                    <th>
                        @Html.Label("LBCLIENT", "Client")
                    </th>
                    <th>
                        @Html.Label("LBENERGY", "Energy")
                    </th>
                    <th>
                        @Html.Label("LBMONEY", "Money")
                    </th>
                </tr>
                @foreach (var item in Model.table)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.company)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.letterCode)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.nic)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.client)
                        </td>
                        <td>
                            @Html.EditorFor(modelItem => item.energy)
                        </td>
                        <td>
                            @Html.EditorFor(modelItem => item.money)
                        </td>
                    </tr>
                }
            </table>
        </div>
        <br />
        <div style="width:100%; text-align:right;">
            <input class="btn-addUser" type="submit" value="Save" />
        </div>
    }
</div>

所以当我运行它并单击我的第一个按钮时,POST Index参数只接收对象的参数部分,表部分为空

当我点击第二个按钮时,POST Save参数在两个属性中都为空。

我尝试了很多不同的方式将模型作为参数进行查找,但没有任何工作。你能告诉我什么是错的吗?我是否以正确的方式使用BeginForm?

提前谢谢。

0 个答案:

没有答案