问题
Good day!
I've existing web service I need to call from controller's action. My web service method looks like this: MyWebServiceMethod(MyWebServiceClass param1)
. Is it possible to use MyWebServiceClass directly in MVC, I mean attach DataAnnotations (the web services classes are partial) for validation and create action like this:
[HttpPost]
public ActionResult MyAction(MyWebServiceClass param1)
{
}
Or I should create separate ViewModel class and copy data to MyWebServiceClass
instance after binding\validation?
回答1:
You should definitely create a view model which is tailored to the needs of the given view which is posting to this controller action. And once you've checked that the model state is valid you could use AutoMapper to convert between this view model and the actual model class that your web service is expecting.
来源:https://stackoverflow.com/questions/4375515/using-soap-web-service-object-as-a-model-in-asp-net-mvc-2