简体   繁体   中英

Returning different types from controller

Is there a way to return models to the client using ajax? In a mvc controller i'm trying to do this;

MyController : Controller
{
    public MyObjectType GetNew()
    {
        return new MyObjectType();
    }
}

This returns to the client just "AssemblName.MyObjectType". Maybe there's some method to override but i don't know the entire framework.

You could serialize the object as JSON and return a JsonResult . At the client you can deserialize it again.

You should call the Json(object) method to serialize your object to JSON.

As Patrick suggested you could return a JsonResult. Otherwise an ApiController will serialize for you.

MyController : ApiController
{
    public MyObjectType GetNew()
    {
        return new MyObjectType();
    }
}

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