How to pass an xml of custom object to ASP.NET MVC Action method via fiddler
I want to test a MVC action method which accpets a custom class parameter via POST (e.g. Book), I have problem passing the parameter via fiddler. Below is my code:
[HttpPost]
public ActionResult BookEdit(BookModel bookModel)
{
...
开发者_高级运维 return View(...);
}
public class BookModel
{
public BookModel()
{
}
public BookModel(Book book)
{
this.Authors = book.Authors;
}
public List<Author> Authors { get; set; }
}
public class Book
{
public List<Author> Authors = new List<Author>();
}
public class Author
{
public string Name { get; set; }
}
Below is the xml that is post to the action method
<BookModel>
<Authors>
<Author>
<Name>1</Name>
</Author>
<Author>
<Name>2</Name>
</Author>
</Authors>
</BookModel>
When I pass the xml, the parameter to the action method is null.
Any idea?
When I pass the xml, the parameter to the action method is null.
Well, that's normal. I can't see any code of yours which would parse this XML into a BookModel object. ASP.NET MVC doesn't do this by default. It does it for JSON, but not XML.
One possibility would be to write a custom Xml value provider as shown here. Another possibility is a custom action filter attribute. Yet another one is a custom model binder.
精彩评论