Not casting my model properly and getting IShapeProxy error

Topics: General, Troubleshooting
Nov 14, 2012 at 4:26 AM
Edited Nov 14, 2012 at 4:32 AM

I have tried a few different things and cannot get my shape to display properly. I am trying to get user input on the front end - ultimately I want it stored in Admin.

ViewModel:

    [Serializable]
    public class AskUsViewModel
    {
        public AskUsViewModel();
        public AskUsPart AskUsPart { get; set; }

        //AskUsRequests is a hook into AdminController.cs
        public List AskUsRequests { get; set; }
        //Page lists in Admin
        public dynamic Pager { get; set; }        
    }

I am using [Serializable] from MVC3/4 code I have in order to persist a wizard. I have a ton of code like this and would rather not redo it all - and was hoping I could jury-rig it into Orchard.

Controller:

        #region Serialization
        private AskUsViewModel askUsData;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var serialized = Request.Form["askUsData"];
            if (serialized != null) //Form was posted containing serialized data
            {
                askUsData = (AskUsViewModel)new MvcSerializer().Deserialize(serialized, SerializationMode.Signed);
                TryUpdateModel(askUsData);
            }
            else
                askUsData = (AskUsViewModel)TempData["askUsData"] ?? new AskUsViewModel();
            TempData.Keep();
        }
        protected override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            if (filterContext.Result is RedirectToRouteResult)
                TempData["askUsData"] = askUsData;
        }
        #endregion

        [Themed]
        public ActionResult Details()
        {
            var askUs = new AskUsViewModel();

            var shape = _orchardServices.New.AskUs_Form_Details(AskUs: askUsData);
            return new ShapeResult(this, shape);
        }

       [HttpPost, Themed]
        public ActionResult Details (string nextButton)
        {
            if ((nextButton != null) && ModelState.IsValid)
                return RedirectToAction("Confirm");
            //return View(askUsData);
            return new ShapeResult(this, _orchardServices.New.AskUs_Form_Details(AskUs: askUsData));
        }

Views/AskUs.Form.Details.cshtml:

@using MyNamespace.Helpers
@using MyNamespace.ViewModels
@using MyNamespace.Models
@using Microsoft.Web.Mvc
@model AskUsViewModel
@{
    //ViewBag.Title = "Ask Us | Step 1: Details";
    //Layout = "~/Views/Shared/_Layout.cshtml";

    //var askUs = Model.ContentItem.As<AskUsPart>();
    //var askUs = (AskUsViewModel) Model.AskUs;
}
                <div class="form">
                    @using (Html.BeginFormAntiForgeryPost())
                    { 
                        @Html.Hidden("askUsData", new MvcSerializer().Serialize(Model, SerializationMode.Signed))
                        <div class="wizard-container">
                            <div class="wizard-step">
                                <div class="wizard-header">
                                    <h3 id="AskUs">
                                        Step 1: Ask Us (Details)
                                    </h3>
                                </div>
                                <div class="wizard-content">
                                    <div class="form_table">
                                        @Html.FullFieldEditor(m => m.AskUsPart.FirstName, new { placeholder = "Your first name" })
                                        @Html.FullFieldEditor(m => m.AskUsPart.EMail)
                                        @Html.FullFieldEditor(m => m.AskUsPart.QuestionComment)
                                    </div>
                                </div>
                            </div>
                            <div class="wizard-navigation">
                                <input name="nextButton" type="submit" value="Next" />
                            </div>
                        </div>
                    }
                </div>

Any help would be much appreciated.

Coordinator
Nov 17, 2012 at 3:11 AM

Why are you using shape results?

You are being inconsistent in that you specify a shape result, but your view expects your view model type as the model. You need to choose one, and either remove the @model directive from your view and access your view model through Model.AskUs, or pass in your view model instead of making a shape result.

Nov 17, 2012 at 1:27 PM
Edited Nov 17, 2012 at 7:08 PM

Thank you. Trying to learn from different examples and I'm not getting it right. Specifically I had trouble with the model in the view and how to handle it. But I finally figured it out with your help.

In my Details ActionResult (AskUsController):

var shape = _orchardServices.New.AskUs_Form_Details(AskUs: askUsData);

the "askUsData" portion is what I need to pass. So in the view, instead of the @model directive (removed it) I went for a variable:

@{
    var askUs = (AskUsViewModel) Model.askUsData;
}

This is where I had the most trouble. I kept passing "AskUs" but it was actually "askUsData". Further in my view:

@Html.FullFieldEditor(m => askUs.AskUsPartRecord.FirstName, new { placeholder = "Your first name" })

Where "AskUsPartRecord" comes from the view model:

public AskUsPartRecord AskUsPartRecord { get; set; }

Thanks.