DirectController

public abstract class DirectController : System.Web.Mvc.Async.AsyncController

Action's Result

TypedJson

DirectController uses its own results serializer that serializes the data by contract. You can explicitly specify the type(contract) of serialized data in this method. More information about JSON serialization can be found in Sencha.Serialization
Using Data Contracts - http://msdn.microsoft.com/en-us/library/ms733127.aspx

public ActionResult GetPost(int postId)
{
	var post = this.Blog.GetPost(postId);

        return TypedJson(post);
}

Form

protected JsonResult Success();
//-> { success: true }

protected JsonResult Form<T>(T data);
//-> { success: true, data: ... }

protected JsonResult Form(ModelStateDictionary state);
//-> { success: true }
//-> { success: false, errors: [{ name: "date", message: "invalid value"}, ...] }

This is response for Ext.form.Panel. You can specify the returned object. In the case of an error you can return Error

[Authorize]
public ActionResult CreatePost(string post)
{
	var postId = this.Blog.CreatePost(post, this.User);

	return Form(new { status = "Post has been created!", id = postId  });
}

[Authorize]
public ActionResult DeletePost(int postId)
{
	var success = this.Blog.DeletePost(postId);

	if(success)
		return Success();
	else
		return Error();
}

Records

protected JsonResult Records();
//-> { success: true, records: [] }

protected JsonResult Records<T>(IEnumerable<T> items);
//-> { success: true, records: [...], total: <items.Count()> }

protected JsonResult Records<T>(IEnumerable<T> items, int total);
//-> { success: true, records: [...], total: ... }

protected JsonResult Records<T>(IEnumerable<T> items, int start, int limit);
//-> { success: true, records: [...], start: ..., limit: ... total: ... }

protected JsonResult Records<T>(IEnumerable<T> items, int? start, int? limit, int? total);
//-> { success: true, records: [...], start: ..., limit: ... total: ... }

This is response for Ext.data.Store. Along with a collection of records ​​you can specify the index of the first record, page size, and the total number of records in the database. In the case of an error you can return Error

public ActionResult GetPosts(int authorId, int start, int limit)
{
	var total = 0;
	var posts = this.Blog.GetPosts(authorId, start, limit, out total);

        return Records(posts, start, limit, total);
}

Error

protected JsonResult Error();
//-> { success: false, isError: true }

protected JsonResult Error(string name, string message);
//-> { success: false, isError: true, name: ..., message: ... }

protected JsonResult Error(Exception exc);
//-> DEBUG { success: false, isError: true, name: ..., message: ... }
//-> RELEASE { success: false, isError: true }

protected JsonResult Error(JsError error);
//-> { success: false, isError: true ... }

protected JsonResult Errors(IEnumerable<JsError> error);
//-> { success: false, errors: [...] }

This is error result for Ext.data.Store or Ext.form.Panel. To return the number of errors you can use Errors method.

public ActionResult DeletePost(int postId)
{
	throw new InvalidOperationException();
	// или
	return Error("Can't do this");
}

// Release build
{
	success: false,
	message: "An error occured",
	isError: true,
	name: "JsError",
	description: /* ... */,
	number: -1,
	helpUrl: null
}
// Debug build
{
	success: false,
	message: /* Exception.Message */,
	isError: true,
	name: "System.InvalidOperationException",
	description: /* Exception.StackTrace */,
	number: /* HRESULT */,
	helpUrl: /* Exception.HelpUrl */
}

Last edited Apr 27, 2012 at 2:23 PM by ShaiHulud, version 4

Comments

No comments yet.