MVC Widget issue rendering objects on edit... - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

MVC Widget issue rendering objects on edit...

  • MVC Widget issue rendering objects on edit...
  • Using a simple index/edit controller I am getting null reference exceptions when making changes and attempting to save entities.  I am not sure how to handle this in an MVC widget with sitefinity as the code works perfectly fine as a standalone MVC project. 
    When a user clicks edit on a product it automatically pulls edit.cshtml into view which retreives the entity by id.  This all works fine, but when a user makes a change and clicks save the Edit controller method that should save the Product throws an exception as the Product parameter is always null.

    Is this because I am using @Html.BeginForm and not @Html.BeginSitefinityForm as mentioned in the documentation?  I am not sure how to get this to register, for other methods you typically need to add a namespace into the web.config under pages/namespaces.  No matter what I attempt to add there VS will not recognize BeginSitefinitiyForm, so any help here would be appreciated as well...

    @model IEnumerable<Product>
        ViewBag.Title = "Index";
        @Html.ActionLink("Create New", "Create")
    @foreach (var item in Model)
                @Html.DisplayFor(modelItem => item.PartNumber)
                @Html.DisplayFor(modelItem => item.SystemDescription)
                @Html.ActionLink("Edit", "Edit", new id=item.PartNumber )

    @model Product
        ViewBag.Title = "Edit";
    @using (@Html.BeginForm())
            @Html.HiddenFor(model => model.PartNumber)
            <div class="editor-label">
                @Html.LabelFor(model => model.SystemDescription)
            <div class="editor-field">
                @Html.EditorFor(model => model.SystemDescription)
                @Html.ValidationMessageFor(model => model.SystemDescription)
                <input type="submit" value="Save" />
        @Html.ActionLink("Back to List", "Index")

    Controller Methods
    //Controller Code
     public ViewResult Index()
                var products = db.Products
                return View(products.Take(5).ToList());
            // GET: /Product/Edit/5
            public ActionResult Edit(string id)
                Product product = db.Products.Find(id);
                return View(product);
            // POST: /Product/Edit/5
            public ActionResult Edit(Product product)
                    //  Product is always null here...
                if (ModelState.IsValid)
                    db.Entry(product).State = EntityState.Modified;
                    return RedirectToAction("Index");
                return View(product);
  • Currently the documentation suggests using @Html.BeginSitefinityForm when declaring an MVC widget.  The documentation declares this incorrectly as it is actually @Html.BeginFormSitefinity.  This resolved my issue with objects rendering as null in the action.
  • Hello Bobby, 
    You have used entity data model in Sitefinity MVC. I am trying to integrate entity data model in my sitefinity MVc application, But not getting a way to do that. Could you please help me to do that, How and where you have created the .edmx file and how you have used it in controller. If possible then please add some code.
  • Hello Bobby,

    I am sorry for the inconvenience and the wrong helper method name.
    I hope that the rendering issues are gone. The BeginSitefinityForm name is changed to BeginFormSitefinity in our documentation. If you find more occurrences, could you please inform us in order to fix them?

    Tanusree, about the entity framework, you can put your edmx file anywhere in the project or even in another client library project and add a reference to it. The controller class should have a using clause leading to the entity models in order to use them. I have provided a usage scenario in your post.

    Tosho Toshev
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items