开发者

Is ToValueProvider() changed in ASP.NET MVC2?

开发者 https://www.devze.com 2022-12-26 08:52 出处:网络
I applied sample codes with the book Pro ASP.NET MVC Framework. [AcceptVerbs(HttpVerbs.Post)] public ViewResult CheckOut(Cart cart, FormCollection form)

I applied sample codes with the book Pro ASP.NET MVC Framework.

[AcceptVerbs(HttpVerbs.Post)]
    public ViewResult CheckOut(Cart cart, FormCollection form)
    {
        if (cart.Lines.Count == 0)
        {
            ModelState.AddModelError("Cart", "Sorry, your cart is empty");
            return View();
        }

        if (TryUpdat开发者_如何学运维eModel(cart.ShippingDetails, form.ToValueProvider()))
        {
            orderSubmitter.SubmitOrder(cart);
            cart.Clear();
            return View("Completed");
        }
        else
        {
            return View();
        }
    }

But when I tried to complete order form in the browser, it showed the following error message:

Value cannot be null or empty. Parameter name: name Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Value cannot be null or empty. Parameter name: name

The sample codes in the book are based on ASP.NET MVC1 but I am running them on VS 2010 with ASP.NET MVC 2. And it seems to be some kind of changes in ToValueProvider() method.

How can I fix this problem and make the mvc application run normally?Thanks very much.


I got the same Exception when I was experimenting with various Data binding techniques in MVC 2.0. My action method:

[AcceptVerbs(HttpVerbs.Post)]
  public ActionResult FindPersonList(FormCollection form)
    {
        PersonList personList = new PersonList();
        if (TryUpdateModel(personList, form.ToValueProvider()))
        {
          //....Do something
        }
        else
        { 
            Add Error message
            return View("SubmittedView");
        }

        return View("GoNextView");
    }


The view Contains only simple strigs and they are not NULL.

The Exception:

System.ArgumentException was unhandled by user code
  Message="Value cannot be null or empty.
  Parameter name: name"
  Source="System.Web.Mvc"
  ParamName="name"
  StackTrace:.....

I followed the suggestion from this link:
ASP.NET MVC 2 problem with UpdateModel

I downloaded Mvc source code and debugged it. I found out that the controllerContext is NULL which causes the above Exception. Why controllerContext is Null I don't know!

I tried to set controllerContext during Unit test using Mock object and it worked fine.

[Test]
public void FindPersonListActionTest()
{
    using (new SessionScope())//Related to ActiveRecord from Castle project ORM
     {
        FormCollection form = new FormCollection();
        form.Add("BaseSearchKey", "Thabet");
         form.Add("DepSearchKey", "Yourtan");
         form.Add("name", "Some name");

          controller.ControllerContext = new FakeControllerContext(controller);
          Assert.IsNotNull(controller.HttpContext, "HttpContext is NULL");
          Assert.IsNotNull(controller.ControllerContext, "ControllerContext is NULL");

          var result = controller.FindPersonList(form) as ViewResult;
          Assert.IsNotNull(result, "ViewResult is NULL");

          var list = (PersonList)((ViewResult)result).ViewData.Model;
          Assert.IsNotNull(list, "Controller Model is NULL");
       }
 }

The same Exception happens when using UpdateModel:

UpdateModel(personList, form);


I think it is a bug, because I tried default binding using action that takes a model as an arg, and behaves normally:

public ActionResult FindPersonList(PersonList personList){....}


And it works fine.

However I would like very much to understand what was set controllerContext to Null.


I was working through the same book (Pro ASP.Net MVC Framework) and resolved the issue by doing the following before my call to CheckOut:

controller.ControllerContext = new ControllerContext();

I'm pretty new to MVC and Testing, so I'm not really sure why the ControllerContext was null, but this will make my tests pass.

0

精彩评论

暂无评论...
验证码 换一张
取 消