Question

Using MVC, in the GET function in the controller I am creating the VM and passing it to the view.

[Themed]
public ActionResult OrderManufacturedProducts(int id)
{   
     QBProductRecord QBproduct = _qbproductService.GetById(id);

     OrderManufacturedProductsVM model = new OrderManufacturedProductsVM(QBproduct);

     return View(model);
}

Then The View:

@model System.ViewModels.OrderManufacturedProductsVM
@{
    Script.Require("ShapesBase");
    Layout.Title = T("Manufactured Orders").ToString();
}

@using (Html.BeginFormAntiForgeryPost())
{
<fieldset>
    <table class="items" summary="@T("This is a table of the manufactured products to be ordered")">
        <colgroup>
            <col id="Col1" />
            <col id="Col2" />
            <col id="Col3" />
        </colgroup>
        <thead>
            <tr>
                <th scope="col">&nbsp;&darr;</th>
                <th scope="col">@T("Name")</th>
                <th scope="col">@T("Description")</th>
            </tr>
        </thead>
        <tbody>  
            <tr>
                <td>@Model.QBProduct.ProductName</td>
                <td>@Model.QBProduct.StockLevel</td>
                <td><input id="Order" name="NoToOrder" type="text" value="0" onchange=""/></td>
            </tr>
        </tbody>
    </table>

     <div class="align right"><button type="submit" name="command" value="Save">Order</button></div>
</fieldset>
}

So the user enters a order no. in the input field and clicks submit which returns to the post.

[HttpPost, ActionName("OrderManufacturedProducts")]
public ActionResult OrderManufacturedProductsPOST(int id, int NoToOrder)
{
// OrderManufacturedProductsVM model = new OrderManufacturedProductsVM(QBproduct);
// return View(model);

return Index();
}

I want to return to the Index page, but it is telling me

Server Error in '/OrchardLocal' Application.
The model item passed into the dictionary is of type 'System.ViewModels.ManufacturedProductsVM', but this dictionary requires a model item of type 'System.ViewModels.OrderManufacturedProductsVM'.

So do I need to use the same VM in my post? all I want to do is reload the index page after the update is made.

NOTE: the update on the record is working fine, I just cant get the correct page to display after.

Was it helpful?

Solution

In your OrderManufacturedProductsPOST action, you should redirect to the action you want to return. Like so:

[HttpPost, ActionName("OrderManufacturedProducts")]
public ActionResult OrderManufacturedProductsPOST(int id, int NoToOrder)
{
    // OrderManufacturedProductsVM model = new OrderManufacturedProductsVM(QBproduct);
    // return View(model);

    return RedirectToAction("Index");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top