ASP.NET MVC CRUD PartialView Popup Issue

Posted by Smiley Face on Stack Overflow See other posts from Stack Overflow or by Smiley Face
Published on 2012-07-11T03:12:06Z Indexed on 2012/07/11 3:15 UTC
Read the original article Hit count: 276

Filed under:
|
|
|

I am creating an MVC website which makes use of Partial Views on Popups to handle all my CRUD transactions. Please note that my application can already handle these CRUD operations perfectly (LINQ-To-Entity). However, I have a problem with my popup forms.

Below is the code from my _Add.cshtml:

@model MyStore.Models.MyModels.ProductsModel

@{
    Layout = null;
}

@using (Ajax.BeginForm("_Add", "Products", new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "POST",
    OnSuccess = "addSuccess"
}, new { @id = "addForm" }))
{
    @Html.ValidationSummary(true)      
    <div id="add-message" class="error invisible"></div>

    <fieldset>
        <legend>Products</legend>

        @Html.HiddenFor(m => Model.ProductCode)

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductName)
            @Html.ValidationMessageFor(model => model.ProductName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>
    </fieldset>
} 

Below is the code from my Controller:

[HttpGet]
public ActionResult _Add(string productCode)
{
    ProductsModel model = newProductsModel();
    model.ProductCode = ProductCode ;
    return PartialView(model);
}

[HttpPost]
public JsonResult _Add(ProductsModel model)
{
    if (ModelState.IsValid)
    {
        ProductsManager prod = new ProductsManager();
        Products pa = new Products();

        pa.ProductCode = model.ProductCode;
        pa.ProductName = model.ProductName;
        pa.Price = model.Price;

        prod.AddProduct(pa);

        return Json(HelperClass.SuccessResponse(pa), JsonRequestBehavior.AllowGet);
    }
    else
    {
        return Json(HelperClass.ErrorResponse("Please review your form"), JsonRequestBehavior.DenyGet);
    }
}

Please note that the _Add.cshtml is a partial view which is being rendered through a Popup.js which I found on the internet. It is rendered through this code:

@Html.ActionLink("[Add Product]", "_Add", new { ProductCode = @ViewData["ProductCode"] }, new { @class = "editLink" })

This works okay. I mean it adds product to my database. But my problem is upon clicking the Proceed button, I get this pop-up download dialog from the page:

Screenshot A

Can somebody please help me with this? I have a hunch it's because of the HttpMethod i'm using (POST, PUT, GET, DELETE) but i'm not really sure which one is right to use or if it really is the problem in the first place.

Any help would be greatly appreciated! PS. Sorry for the long post.

© Stack Overflow or respective owner

Related posts about mvc

Related posts about popup