首页 > 解决方案 > C# 和 ASP.NET MVC 5 / Web API

问题描述

我有一个在gridview 中显示来自外部Web API 的数据的视图。从网格中选择记录时,代码导航到详细信息页面/视图。

详细信息视图显示其他人口统计数据。我需要从详细信息页面提交详细信息表单并获取一些将传递给我的控制器的数据元素,以便我可以将事务发布到外部 Web API。

我目前在控制器中获得“空”值。错误是:

参数字典包含参数“id”的空条目

我需要将参数传递给控制器​​吗?帮助表示赞赏。

模型.cs:

using System.Collections.Generic;

namespace CPA.Models
{
    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class AlertMessage
    {
        public string Title { get; set; }
        public string Message { get; set; }
        public string Severity { get; set; }
    }

    public class ItemLine
    {
        public string Description { get; set; }
        public int BilledAmount { get; set; }
        public int PaidAmount { get; set; }
    }

    public class CitationDetail
    {
        public string CitationNumber { get; set; }
        public string DefendantName { get; set; }
        public string DateofBirth { get; set; }
        public string VehicleTagNumber { get; set; }
        public string CaseType { get; set; }
        public string CaseStatus { get; set; }
        public string AppearanceDate { get; set; }
        public bool IsPayable { get; set; }
        public int FineSuspended { get; set; }
        public int FineServed { get; set; }

        public List<AlertMessage> AlertMessages { get; set; }
        public List<ItemLine> ItemLines { get; set; }
    }

    public class Root
    {
        public List<CitationDetail> CitationDetails { get; set; }
        public int CitationCount { get; set; }
        public bool SuccessfulSearch { get; set; }
        public string ErrorMessage { get; set; }
    }
}

看法:

<h2>Details</h2>

<body>
@using (Html.BeginForm("Index3", "Citation", FormMethod.Post))
{
    <table width="50%">
        <tr>
            <td>
                @Html.DisplayNameFor(model => model.CitationNumber)
                <input type="text" id="txtcitationnber" value=@Html.DisplayFor(model => model.CitationNumber) />
            </td>
            <td>
                @Html.DisplayFor(model => model.CitationNumber)
            </td>
        </tr>
        <tr>

            <td>
                @Html.DisplayNameFor(model => model.DefendantName)
            </td>

            <td>
                @Html.DisplayFor(model => model.DefendantName)
            </td>
        </tr>
        <tr>

            <td>
                @Html.DisplayNameFor(model => model.DateofBirth)
            </td>

            <td>
                @Html.DisplayFor(model => model.DateofBirth)
            </td>
        </tr>

        <tr>
            <td>
                @Html.DisplayNameFor(model => model.VehicleTagNumber)
            </td>

            <td>
                @Html.DisplayFor(model => model.VehicleTagNumber)
            </td>
        </tr>

        <tr>
            <td>
                @Html.DisplayNameFor(model => model.CaseType)
            </td>

            <td>
                @Html.DisplayFor(model => model.CaseType)
            </td>
        </tr>

        <tr>
            <td>
                @Html.DisplayNameFor(model => model.CaseStatus)
            </td>

            <td>
                @Html.DisplayFor(model => model.CaseStatus)
            </td>
        </tr>

        <tr>
            <td>
                @Html.DisplayNameFor(model => model.AppearanceDate)
            </td>
            <td>
                @Html.DisplayFor(model => model.AppearanceDate)
            </td>
        </tr>

        <tr>
            <td>
                @Html.DisplayNameFor(model => model.IsPayable)
            </td>
            <td>
                @Html.DisplayFor(model => model.IsPayable)
            </td>
        </tr>
        <tr>
            <td>
                @Html.DisplayNameFor(model => model.FineSuspended)

            </td>
            <td>
                @Html.DisplayFor(model => model.FineSuspended)
            </td>
        </tr>
        <tr>
            <td>
                @Html.DisplayNameFor(model => model.FineServed)
            </td>
            <td>
                @Html.DisplayFor(model => model.FineServed)
            </td>
        </tr>
        <tr>
            <td>
                <br />
            </td>
        </tr>

        <tr bgcolor=#c0c0c0>
            <td>
                @Html.DisplayNameFor(model => model.AlertMessages)
            </td>
        </tr>
        @if (@Model.AlertMessages != null)
        {
            for (var i = 0; i < Model.AlertMessages.Count; i += 1)
            {
                <tr>

                    <td>
                        @Html.DisplayFor(model => model.AlertMessages);
                    </td>
                </tr>
            }
        }

        <tr>
            <td>
                <br />
            </td>
        </tr>

        <tr bgcolor="#ffc426">
            <td>Description </td>
            <td> BilledAmount</td>
            <td> PaidAmount</td>
        </tr>
        @if (@Model.ItemLines != null)
        {
            for (var i = 0; i < Model.ItemLines.Count; i += 1)
            {

                <tr>
                    <td>@Html.DisplayFor(model => model.ItemLines[i].Description) </td>
                    <td> @Html.DisplayFor(model => model.ItemLines[i].BilledAmount)</td>
                    <td> @Html.DisplayFor(model => model.ItemLines[i].PaidAmount);</td>
                </tr>

            }
        }

        <tr>
            <td>
                <br />
            </td>
        </tr>

        <tr bgcolor="#c0c0c0">
            <td>Charge Code </td>
            <td> Payment Amount</td>
            <td>  </td>
        </tr>

        <tr>
            <td>
                <select id="chargecodes">
                    <option value="500002">BF</option>
                    <option value="5000BT">BT</option>
                    <option value="500007">BV</option>
                    <option value="5000RE">RE</option>
                    <option value="500001">TC</option>
                    <option value="500004">CB</option>
                    <option value="500008">MI</option>
                    <option value="500011">OT</option>s
                    <option value="500012">PB</option>
                    <option value="500010">PK</option>
                    <option value="500009">PP</option>
                    <option value="500005">SB</option>
                </select>
            </td>
            <td><input type="text" id="paymentamount" onkeypress="return isNumberKey(event)" /></td>
            <td><input type="submit" value="Submit" /></td>
        </tr>

    </table>
}
</body>
   

控制器:

[HttpPost]
public ActionResult Index3(CitationDetail citation)
// public ActionResult Index(FormCollection citation)
{
    try
    {
        string citationnbr = citation.CitationNumber;

        ViewData["CitationNumber"] = citationnbr;

        var client2 = new RestClient("http://xx.xx.xx.xx:8109/api/xxxxx/Citation/PostTransaction");
        client2.Timeout = -1;

        var request2 = new RestRequest(Method.POST);
        request2.AddHeader("Content-Type", "application/json");

        // Creating Json object
        // JObject jObjectbody = new JObject();
        // jObjectbody.Add("ValidatePayment", "Validate");
        // jObjectbody.Add("ValidatePayment", "Payment");
        // jObjectbody.Add("TransactionType", chargenbr);
        // jObjectbody.Add("CitationNumber", citation.CitationNumber);
        // jObjectbody.Add("TransactionAmount", pmt);
        // jObjectbody.Add("MachineNumber", "24");
        // jObjectbody.Add("CashierNumber", "25");
        // jObjectbody.Add("CheckNumber", "0");
        // jObjectbody.Add("CloseOut", "Y");
        // jObjectbody.Add("ReceiptNumber", "0");
        // jObjectbody.Add("RevenueType", chargecode);
        // jObjectbody.Add("TransactionDate", DateTime.Now);

        // request2.AddParameter("application/json", jObjectbody, ParameterType.RequestBody);
        // IRestResponse response2 = client2.Execute(request2);

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

标签: c#asp.net-web-apiasp.net-mvc-5

解决方案


我认为这个问题来自你的观点。

首先,您必须精确到要使用哪个模型的视图。因此,在您的视图顶部,您可以编写:

@model CPA.Models.CitationDetail

在您看来,您只是显示“CitationDetail”属性名称,因此当您提交表单时,您不会发送任何数据,因为您没有任何输入。

要发送数据,您需要使用以下 html 助手:

@Html.DisplayFor(model => model.YourAttribute)
@Html.EditorFor(model => model.YourAttribute)

这个 html 助手将生成以下 html 代码:

<label for="YourAttribute">YourAttribute</label>
<input id="YourAttribute" name="YourAttribute" value="">

您只需要为您的类的每个属性添加一个输入到您的表单中。

例如,在您看来,属性“CitationNumber”的正确表单输入将是:

@Html.DisplayFor(model => model.CitationNumber)
@Html.EditorFor(model => model.CitationNumber)

推荐阅读