c# - 从类型 'System.String'到类型 '' Y'的参数转换失败,因为没有类型转换器可以在这些类型之间转换

标签 c# .net asp.net-mvc-4

我被这个错误困住了,我检查过值不为空。当我尝试点击“保存”按钮时出现此错误。

请帮帮我,这是错误:

The parameter conversion from type 'System.String' to type 'ULIV.ViewModels.NewProposal' failed because no type converter can convert between these types.

ModelState.IsValid 为假。

这是使用的 View 模型:

public class PurchaseOrderViewModel
{
    [Key]
    public int ID { get; set; }

    public IEnumerable<Institution> Institution { get; set; }

    [Required]
    public string PurchaseOrderNo { get; set; }

    [Required]
    public DateTime PurchaseOrderDate { get; set; }

    [Required]
    public DateTime ReceivedDate { get; set; }

    public string Remarks { get; set; }

    public IEnumerable<NewProposal> Proposal { get; set; }


    public IEnumerable<PODetail> PODetail { get; set; }

    public IEnumerable<NewProposal> AddedProposal { get; set; }


}

这是 Controller Action :

[HttpPost]
    public ActionResult Create(PurchaseOrderViewModel purchaseorderviewmodel)
    {

        purchaseorderviewmodel.AddedProposal  = (List<ULIV.ViewModels.NewProposal>)Session["AddedProposal"];



        //purchaseorderviewmodel.Proposal = db.NewProposal.Where(x => x.Status.StatusID == 3).ToList();



        int instituionId = Convert.ToInt32(Session["InstitutionID"]);

        purchaseorderviewmodel.Institution = db.Institutions.Where(x => x.InstitutionID == instituionId).ToList();

        if (ModelState.IsValid)
        {
            db.PurchaseOrderViewModels.Add(purchaseorderviewmodel);
            db.SaveChanges();
            return RedirectToAction("Index","Fulfillment");
        }


        ViewBag.Proposal = new SelectList(db.NewProposal.Where(x => x.Status.StatusID == 3), "ProposalID", "ProposalCode");
        return View(purchaseorderviewmodel);
    }

此外,这些是即时窗口中的值:

((List<ULIV.ViewModels.NewProposal>)Session["AddedProposal"]).FirstOrDefault()

DateModified: {1/15/2014 2:57:52 PM}
FinalUnitPrice: 23
FinalVolume: 34
getdatestring: "1/15/2014"
Institution: {System.Data.Entity.DynamicProxies.Institution_B01649AB79941CE0188D081B425698F4FD6629E82FDB13CCE17B03E348459273}
InstitutionID: 1
Product: {ULIV.Models.ProductModel}
ProductID: 1
ProposalCode: "PC-2014-00001"
ProposalID: 1
ProposedUnitPrice: 3
ProposedVolume: "2"
RelatedProposalCode: ""
Remarks: "hello"
Status: {ULIV.Models.Status}
StatusID: 3
VaccineType: {ULIV.Models.VaccineType}
VaccineTypeID: 1
((List<ULIV.ViewModels.NewProposal>)Session["AddedProposal"]).FirstOrDefault()

这是 View :

<div class="row">
@using (Html.BeginForm("Create", "PurchaseOrder",
        new { ReturnUrl = ViewBag.ReturnUrl },
        FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <h1 class="subpage-headline font-global">New Purchase Order</h1>
    <div class="row">
        <h2 class="subpage-headline2 font-global">New Purchase Order</h2>
        @*<form class="form-horizontal" role="form">*@
            @foreach (var inst in Model.Institution)
            {
                <div class="row-div">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Institution Name:</label>
                        <div class="col-sm-3">
                            <label class="control-text-label">@Html.DisplayFor(modelItem => inst.InstitutionName)</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Account Classifications:</label>
                        <div class="col-sm-3">
                            <label class="control-text-label">Private</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Employee Population:</label>
                        <div class="col-sm-3">
                            <label class="control-text-label">50</label>
                        </div>
                    </div>
                </div>
                <div class="row-div">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Address:</label>
                        <div class="col-sm-4">
                            <label class="control-text-label">Ninoy Aquino Ave. Clark Pampanga Clark Freeport Zone 1500</label>
                        </div>

                    </div>

                    <div class="form-group">

                        <label class="col-sm-2 control-label">Telephone No.:</label>
                        <div class="col-sm-3">
                            <label class="control-text-label">599-3146</label>
                        </div>
                    </div>
                </div>
            }
        @*</form>*@
        @*<form class="form-horizontal" role="form">*@
            <div class="form-group">
                <label class="col-sm-2 control-label">Purchase Order Number:</label>
                <div class="col-sm-4">

                    @Html.TextBoxFor(model => model.PurchaseOrderNo, new { @class = "form-control", @type = "text" })
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Purchse Order Date:</label>
                <div class="col-sm-4">
                    @Html.TextBoxFor(model => model.PurchaseOrderDate, new { @class = "form-control datepicker", @type = "text" })
                </div>
            </div>
            <div class="row">
                <label class="col-sm-2"></label>
                <label class="col-sm-2 font-md">Proposal Code <span class="required">*</span></label>
                <label class="col-sm-2 font-md">Description <span class="required">*</span></label>
                <label class="col-sm-2 font-md">Unit Price w/ VAT <span class="required">*</span></label>
                <label class="col-sm-1 font-md">Total Units <span class="required">*</span></label>
                <label class="col-sm-2 font-md">Total Amount w/ VAT <span class="required">*</span></label>
                <label class="col-sm-1"></label>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Proposal:</label>
                <div class="col-sm-2">


                    @Html.DropDownList("ProposalID", new SelectList(ViewBag.Proposal, "Value", "Text"), "SELECT Proposal", new { @class = "form-control input", @type = "text", @name = "ddlInst" })


                </div>


                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="prodDesc_0">
                    </div>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="unitPrice_0">
                    </div>
                    <div class="col-sm-1">
                        <input type="text" class="form-control" id="totalUnits_0">
                    </div>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="totalAmount_0">
                    </div>
                    <div class="col-sm-1">
                        <button type="button" class="btn btn-primary" onclick="AddProposal()">Add</button>
                    </div> 

            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Product:</label>
                <div class="col-sm-9">
                    <table class="table table-bordered table-hover">
                        <thead>
                            <tr class="th-center">
                                <th>Proposal Code</th>
                                <th>Description</th>
                                <th>Unit Price VAT</th>
                                <th>Total Units</th>
                                <th>Total Amount w/ VAT</th>
                                <th>Action</th>
                            </tr>
                        </thead>

                        <tbody>
                            @Html.HiddenFor(model => model.AddedProposal)
                            @if (Model.AddedProposal != null)
                            {
                                foreach (var prop in Model.AddedProposal)
                                {
                                <tr class="text-center">
                                    <td>@Html.DisplayFor(modelItem => prop.ProposalCode)</td>
                                    <td>@Html.DisplayFor(modelItem => prop.Product.Description)</td>
                                    <td>@Html.DisplayFor(modelItem => prop.FinalUnitPrice)</td>
                                    <td>@Html.DisplayFor(modelItem => prop.FinalVolume)</td>
                                    <td>3875.75</td>
                                    <td class="text-center">
                                        <a href="#">
                                            <img src="../../Images/icon_delete.png" width="16" height="16"></a>
                                    </td>
                                </tr>
                                }
                            }
                            @if (Model.AddedProposal == null)
                            {
                            }
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Received Date:</label>
                <div class="col-sm-4">
                    @Html.TextBoxFor(model => model.ReceivedDate, new { @class = "form-control datepicker", @type = "text" })
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Remarks:</label>
                <div class="col-sm-4">
                    @Html.TextBoxFor(model => model.Remarks, new { @class = "form-control", @type = "text" })
                </div>
            </div>
        @*</form>*@
    </div>
    <div class="row col-xs-offset-2" style="padding-top: 30px;">
        <button type="submit" class="btn btn-primary">Save</button>
        <button type="button" class="btn btn-primary" onclick="window.location='@Url.Action("Index", "Fulfillment")'">Cancel</button>
    </div>
}

第一次遇到这种错误,求助。我不确定错误是否来自“”

最佳答案

明白了!

我刚刚删除了这行代码:

@Html.HiddenFor(model => model.AddedProposal)

关于c# - 从类型 'System.String'到类型 '' Y'的参数转换失败,因为没有类型转换器可以在这些类型之间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21229212/

相关文章:

c# - 如何以自己的色调显示颜色 channel ?

asp.net-mvc - 检测用户 session 超时时刻并执行方法

c# - Nhibernate 是否使用 ADO.NET 连接数据库?

.net - .Net 中奇怪的随机数错误

javascript - Asp.net WebApi Aspose.Cells - 导出和下载 Excel

.net - Azure WebJobs 有很多 QueueTrigger 不好的做法吗?

asp.net-mvc-4 - SimpleMembershipProvider : webpages_Membership table

C# ASP.NET MVC 4 Web API XmlDocumentationProvider 错误

c# - 如何从 C# 字符串中按名称查找文本框控件?

c# - 在 Cake 构建脚本中提交计数?