Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • klaus 15 posts 35 karma points
    Mar 05, 2021 @ 11:49
    klaus
    0

    Umbraco How to parse a form POST to surface controller?

    Hi I have an Ajax.beginform that POST a ProductName to a surface controller, or at least this is what i am looking for. I can't seem to get hold of the Model. I can pass simple values with routeValues but how do I pass complex objects to the controller? I am getting an Internal Server Error 500 with the following controller:

    public class NetShopController : RenderMvcController
        {
    
            public ActionResult AddProductForm(Product model)
            {
    
                var mod = model;// <-- Nothing in it and it returns a 500 error
                return PartialView("~/Views/Partials/NetShop/result.cshtml", new HtmlString(Session["test"].ToString()));
    
            }
        }
    

    The following is my View and as you can see I am only trying to POST one input field with Ajax.BeginForm

    @inherits UmbracoViewPage<Product>
    @using ContentModels = Umbraco.Web.PublishedModels;
    
    @{ Layout = "master.cshtml"; }
    <!-- for the section we want to show the shop header -->
    @Html.Partial("~/Views/Partials/SectionHeader.cshtml", Model.Parent)
    
    <section class="section">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <div class="product-image-container">
                        <img class="product-image" src="@Model.Photos.Url()" alt="@Model.ProductName image" />
                    </div>
                </div>
                <div class="col-md-6">
                    <h1>@Model.ProductName</h1>
                    <div class="product-price">@Model.Parent.GetProperty("DefaultCurrency").Value() @Model.Price.ToString("F")</div>
                    <div class="product-teaser">@Model.Description</div>
                    <div class="product-button">
                        @using (Ajax.BeginForm(actionName: "AddProductForm", controllerName: "NetShopBasketSurface", routeValues: new { product = Model }, ajaxOptions: new AjaxOptions()
                        {
                            UpdateTargetId = "form-result",
                            HttpMethod = "post",
                            InsertionMode = InsertionMode.Replace,
                            OnSuccess = "addProductForm.showResult",
                            OnFailure = "addProductForm.showResult"
                        }, htmlAttributes: new { id = "pform" }))
                        {
                            <div>
                                @Html.HiddenFor(m => m.ProductName)
                                <button class="product-submit button button--border--solid" type="submit" id="addProductButton">Tilføj til kurv</button>
                            </div> @*<button class="button button--border--solid">Tilføj til indkøbskurv</button>*@
                        }
                                </div>
                    <div class="product-advantages">
                        @if (Model.Features != null)
                        {
                            foreach (var feature in Model.Features)
                            {
        <div class="product-advantage">
            <h4>@feature.GetProperty("featureName").Value()</h4>
            <h5>@feature.GetProperty("featureDetails").Value()</h5>
        </div>}
                        }
                    </div>
                </div>
            </div>
        </div>
    </section>
                    <section class="section section--sand">
                        <div class="container">
                            <!-- todo: Check if grid is empty via a property value converter -->
                            @Html.GetGridHtml(Model, "bodyText", "bootstrap3-fluid")
                        </div>
                    </section>
    
    
    <div id="form-result"></div>
    

    I am stuck!

    Thanks

  • James Shelley 9 posts 103 karma points
    Mar 05, 2021 @ 15:48
    James Shelley
    0

    Hey Klaus

    Can you pass in ContentModel model as a parameter to your method instead?

    Then could you use

      var yourProductModel = new Product(model.Content);
      yourProductModel.SomeFeature = _someService.GetSomeFeature etc etc
    

    Is this any help at all?

Please Sign in or register to post replies

Write your reply to:

Draft