简体   繁体   中英

Return PartialView from another function in ASP.NET MVC3 C#

I'm not sure if what i'm trying to achieve is even possible, but I'm trying to create a generic application that will return a PartialView dependent on the action submitted from the user. Unfortunately, the following code throws the following error when I run it:

The model item passed into the dictionary is of type 'System.String', but this dictionary requires a model item of type 'Core.Entities.CGrid'

On my view, I am using the following jQuery which hooks into any anchor tags:

$("a").live('click', function (e) {
            if ($(this).attr("href").indexOf("#func_") > -1) {           {
                var func = '/Core/FunctionCall?function=' + $(this).attr("href").replace("#func_", "");
                $.ajax({
                    url: func,
                    type: 'GET',
                    async: false,
                    success: function (result) {
                        $("#customerTabBody").empty();
                        $("#customerTabBody").html(result);
                    }
                });
            }
        });

This works and has been tested returning the FunctionCall.cshtml placeholder page. However, I have since modified the FunctionCall function to resemble below (this is work in progress):

public class CoreController : Controller
{
    [HttpGet]
    public ActionResult FunctionCall(string function)
    {
        Core.Entities.CFunction func = null;

        try
        {
            using (CFunction f = new CFunction())
                func = f.GetFunction(Convert.ToInt32(function));

            if (func.ModuleName == "GRID")
                return PartialView("GridView", func.Parameters[1].ToString());
            else
                return PartialView("FormView", func.Parameters[1].ToString());
        }
        catch (Exception)
        {
            return null;
        }
    }

    [HttpGet]
    public ActionResult GridView(string gridView)
    {
        Core.Entities.CGrid grid = null;

        using (CGrid data = new CGrid()) 
            grid = data.GetGridNew(gridView);

        return PartialView(grid);
    }

    [HttpGet]
    public ActionResult FormView(string formView)
    {
        Core.Entities.CForm form = null;

        using (CForm data = new CForm())
            form = data.GetForm(formView);

        return PartialView(form);
    }
}

If I call the functions "GridView" and "FormView" directly then the partial view is displayed correctly with no errors, however rather than cater for every possible type of view that may be needed in the jQuery (or the functions that build up the anchor links) I wanted to have a single function that would handle the request and return a view based on whatever value was passed. The problem seems to be returning these PartialViews from within another function.

Any help is greatly appreciated.

ETA: Thanks @YD1m (and everyone else that replied). I had assumed that the code would jump straight into the "GridView" or "FormView" functions as opposed to just returning the view. I think more coffee is needed :)

Here is the revised code that now works:

[HttpGet]
    public ActionResult FunctionCall(string function)
    {

        Core.Entities.CFunction func = null;

        try
        {
            using (CFunction f = new CFunction())
                func = f.GetFunction(Convert.ToInt32(function));

            if (func.ModuleName == "CORE")
            {
                Core.Entities.CGrid grid = null;

                using (CGrid data = new CGrid())
                    grid = data.GetGridNew(func.Parameters[1].ToString());

                return PartialView("GridView", grid);
            }
            else
            {
                Core.Entities.CForm form = null;

                using (CForm data = new CForm())
                    form = data.GetForm(func.Parameters[1].ToString());

                return PartialView("FormView", form);
            }
        }
        catch (Exception)
        {
            return null;
        }
    }

You pass to view the model of wrong type. Check type of model in @model statement in view file and make sure that you send apt model from this code:

if (func.ModuleName == "GRID")
                return PartialView("GridView", func.Parameters[1].ToString());
            else
                return PartialView("FormView", func.Parameters[1].ToString());
return PartialView("GridView", func.Parameters[1].ToString());

This line does not actually call the GridView actionmethod, it simply returns the partial view named "GridView" and passes in the second parameter as the model. As YD1m pointed out, this is causing an error because the view is not set up to accept a string as it's model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM