MVC Widget issue rendering objects on edit...

Posted by Community Admin on 04-Aug-2018 01:58

MVC Widget issue rendering objects on edit...

All Replies

Posted by Community Admin on 09-Oct-2012 00:00

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...

Index.cshtml

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

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

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
        [HttpPost]
        public ActionResult Edit(Product product)
        
                //  Product is always null here...
            if (ModelState.IsValid)
            
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            
            return View(product);
        

Posted by Community Admin on 12-Oct-2012 00:00

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.

Posted by Community Admin on 13-Aug-2013 00:00

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.

Posted by Community Admin on 16-Aug-2013 00:00

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.

Regards,
Tosho Toshev
Telerik

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

This thread is closed