user3201367
user3201367

Reputation: 141

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

I'm stucked with this error, I checked values are not null. This error came out when I tried to hit on the Save button.

Please help me, this is the error:

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

ModelState.IsValid is false.

Here's the View Model used:

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; }


}

This is the 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);
    }

Also, these are the values on the immediate window:

((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()

This is the 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>
}

Please help this is the first time I encounter this kind of error. I'm not sure if the error comes from the ""

Upvotes: 4

Views: 6039

Answers (4)

Nicholas Petersen
Nicholas Petersen

Reputation: 9558

I just found a very ornery cause for this error.

public async Task<ActionResult> Add(string m = null) { }

[HttpPost]
public async Task<ActionResult> Add(FooViewModel m) { }

In the Get method I have an optional string m = null argument, meanwhile, the Post method of course has the model passed in. Well, because in this case I named that variable m as well, that's what caused this error. Ugly and maybe a bug?

Upvotes: 1

Fred Ramsey
Fred Ramsey

Reputation: 23

For me, it was referencing a type rather than a field. What I had was:

Html.DropDownListFor(m => m.SiteExtendedAttributeValues[0] etc.

I changed it to:

Html.DropDownListFor(m => m.SiteExtendedAttributeValues[0].SiteExtendedAttributeDefinitionId

Error went away.

Upvotes: 0

user3201367
user3201367

Reputation: 141

GOT IT!

I just removed this line of code:

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

Upvotes: 7

twinmind
twinmind

Reputation: 1610

there's probably string value in Session["AddedProposal"], so when you try to explicitly convert it to a NewProposal you get an error

Upvotes: -1

Related Questions