How to use ModelState.IsValid with complex objects? - asp.net-mvc-2

I'm using NHibernate and I've got a Campaign class and it has a Client class as one of its members and I'm attempting to use ModelState.IsValid in my [HttpPost] public Create (CreateCampaignViewModel vm) method.
As my ViewModel contains the object Campaign ModelStats.IsValid is always false as I'm passing the Client.Id from the form instead of the entire Client object.
Is there a way to have it load the Client member before it runs the validation code that sets ModelState.IsValid? Or is there a way to refresh the state of ModelState after I 'manually' retrieve the client object based on the ClientId and bind it to the Campaign?

You probably want to add an attribute to the parameter of your action method, to disable binding of the Id:
[HttpPost]
public Create([Bind(Ignore="ClientId")]CreateCampaignViewModel vm)

Steven Sanderson has a blog post on just this. He removes any of the items which do not have incoming values from the errors collection.
http://blog.stevensanderson.com/2010/02/19/partial-validation-in-aspnet-mvc-2/

Try using one of the overloads on the controller's UpdateModel method. Then call ModelState.IsValid.

I believe SharpArch has a modelbinder that can load the NH objects. But, I don't like that idea very much...instead I'd use a separate PostModel to hold the posted form values. If you re-use the ViewModel to also hold the post data, you'll usually have a lot of fields that make no sense to be posted (like the Client object in your example) or readonly display fields.
class CampaignPostModel{
...
public int ClientId { get; set; }
}
All you need to do is match-up the property names to the form input names and add this as a parameter on your post action method.

Related

How to access the value of a Property argument from nested activities?

I'm sure there's something I'm missing here, but a lot of Googling hasn't uncovered it for me. The situation is like this:
We created a custom workflow designer that allows end users to build workflow definitions from various custom activities we define (Review, Submit, Notify, etc). These definitions (Xaml) get saved off to a Db and used to create workflow instances for long running processes in our system. The users can set properties on each of them (e.g. Review has a property argument: AllowedRoles). The problem is, I'm not able to pass those properties on to nested activities.
For example:
Review has an internal activity 'WriteStatus' that needs access to the 'AllowedRoles' property on Review. If 'AllowedRoles' is defined as a Property, WriteStatus can't "see" it to assign it's value. I can change it from a Property to an InArgument, but then I'm not able to map values to and from the property in the designer (these properties should be part of the definition, and not associated with any specific context).
Has anyone faced this issue or have advice on how I could approach the problem differently?
Thanks in advance!
Royce
I was able to get around the property vs InOurArgument problem by converting the XAML activities to code. This allowed me to set the properties on activities in code, and then pass them to inner activities inline. There may be a better way, but it's working out well so far.
public sealed class Test : Activity
{
public string Stuff { get; set; } // CLR Property
public Test()
{
Implementation = () => new WriteLine {Text = Stuff};
}
}

Combining URL and POST variables in ServiceStack

I am trying to convert an existing wcf rest api to ServiceStack, and having issues right out of the gate:
[Route("foo/{userId}","POST")]
public class MyInputModel : IReturnVoid
{
public string userId { get; set; }
public SomeOtherObject properties { get; set; }
}
The intention here is that I would provide the userId in the url, and an instance of SomeOtherObject in the post body. The error I get is
<Message>Could not deserialize 'application/xml' request using MyInputModel'
Error: System.Runtime.Serialization.SerializationException:
Error in line 1 position 42. Expecting element 'MyInputModel'
from namespace 'blahblahblah'.. Encountered 'Element' with name
'SomeOtherObject', namespace 'http://blahblahblah'.
The only things I can think of are to wrap my xml in a MyInputModel to make the serializer happy. This is not really an option for backwards compatibility.
I could also modify SomeOtherObject to be the top level input model, and put a UserId property in there, but this also feels suboptimal since it is an object used throughout the api, and is really not tied to a user id. It is also already published independently, so it would be painful to make changes there.
Is there any way to indicate that the root element of the posted data will be a SomeOtherObject insted of a MyInputModel? In WebApi this would be with the [FromBody] attributes and whatnot. Does servicestack have anything similar?
The purpose of a DTO is to auto-generate the wire format which is why ServiceStack requires the Request DTO to match the shape of the incoming request. Part of what makes ServiceStack so productive is that it's a code-first web service framework which encourages starting from C# and projecting out, i.e. your clients should bind to your web service outputs and not the other way round of mapping code-first models to existing schema inputs.
Having said that, the Serialization / Deserialization wiki page lists the different ways to override ServiceStack's default request binding with your own.
Access HTTP Request variables in any Service or Filter
Not everything needs to be mapped to a DTO as any HTTP Variable can still be accessed from the IHttpRequest available from any service or filter, i.e:
base.Request.QueryString
base.Request.FormData
base.Request.Headers[name]
base.Request.PathInfo
base.Request.AbsoluteUri

MVC2 controller method that can accept unknown number of variables?

I need to work with the below posted data, or a variant thereof. Basically, I need to post a variable number of key-value pairs that represent a question id and answer string.
How do I write an ASP.NET MVC2 controller method signature to accept an unknown number of key-value pairs?
attachmentId=8809&question_712=&question_713=&question_714=&question_715=&question_716=&question_717=&question_719=&question_720=&question_721=&question_722=&question_723=&question_724=&question_725=&question_726=&question_727=&question_731=&question_738=&question_739=&question_741=&question_742=&question_743=&question_744=&question_745=&question_746=&question_747=&question_748=
Please note that in this example, there are 26 question keys with empty values. There may be more or less keys and they may or may not have a value. I can reformulate the way the data is sent by the client, so if the best solution is to rethink the way it is sent, I'm open to that.
This is basically the data a FormCollection collects. It's used in the automatically generated controllers by default. i.e. public ActionResult Edit(int id, FormCollection collection)
Use an array. The default modelbinder can detect arrays.
model.questions[0].key model.questions[1].value and so on for the html tag names then build an object that follows those conventions.
public class QuestionUpdateModel{
public int attachmentID{get;set;}
public QuestionPair[] Questions{get;set;}
}
public class QuestionPair{
public int key{get;set;}
public string value{get;set;}
}
After that your controller should accept an argument of QuestionUpdateModel type. The modelbinder should take care of the rest. Make sure you index them sequentially so it can create the array without null entries.

ViewModel not matching model causes modelstate to be invalid

I have a model class like this:
class Person {
string FirstName,
string LastName,
string ID
}
When I send the model to the browser via a GET, I send the data as a composite of two fields (e.g. FirstName.ToString() + LastName.ToString()) through an anonymous type.
The problem comes when I do a POST back to the server. Since the JSON is coming back as different from the model, it comes back as invalid through ModelState.IsValid() because my action method is expecting a List<Person> persons.
I really don't want to create a ModelViewModel duplicating code, because one field is causing the model to be invalid. Is there a way around this?
In this case, I would add DataAnnotations to the ViewModel class and change the Action to accept the ViewModel rather than the Model.
Once you validate that the ViewModel being passed to the Action is valid, you can parse the composite field back to into the First and Last names properly.

MVC2 Action to handle multiple models

I've been looking around for an answer to this, which I can't believe hasn't been asked before, but with no luck I'm attempting here.
I have a signup form which differs slightly based upon what type of participant the requester is. While writing tests for the solution, I realized that all actions did the same things, so I'm attempting to combine the actions into one using a strategy pattern.
public abstract class BaseForm { common properties and methods }
public class Form1 : BaseForm { unique properties and overrides }
....
public class FormX : BaseForm { unique properties and overrides... in all about 5 forms }
Here is the combined action:
[ModelStateToTempData, HttpPost]
public ActionResult Signup(int id, FormCollection collection)
{
uiWrapper= this.uiWrapperCollection.SingleOrDefault(w => w.CanHandle(collection));
// nullcheck on uiWrapper, redirect if null
var /*BaseForm*/ form = uiWrapper.GetForm(); // Returns FormX corresponding to collection.
this.TryUpdateModel(form, collection.ToValueProvider()); // Here is the problem
form.Validate(this.ModelState); // Multi-Property validation unique to some forms.
if (!this.ModelState.IsValid)
return this.RedirectToAction(c => c.Signup(id));
this.Logic.Save(form.ToDomainClass());
return this.RedirectToAction(c => c.SignupComplete());
}
The problem I'm having is that TryUpdateModel binds only the common properties found in BaseForm. My previous code used public ActionResult FormName(int id, FormX form) which bound properly. However, I did some testing and discovered that if I replace var form with FormX form the form binds and everything works, but I'm back to one action per form type.
I'm hoping to find a way to get this to bind properly. form.GetType() returns the proper non-base class of the form, but I'm not sure of how to grab the constructor, instantiate a class, and then throw that into TryUpdateModel. I know that the other possibility is a custom ModelBinder, but I don't see a way of creating one without running into the same FormBase problem.
Any ideas or suggestions of where to look?
I was trying to do something similar to Linq, I was trying to create a class that would inherit some standard fields (ID, etc). I found that the default Linq engine would only use fields from the instantiated class, not from any inherited classes or interfaces.
To construct a Type simply use code like:
var form = Activator.CreateInstance(uiWrapper.GetForm());
I figured it out!
Erik's answer wasn't the solution, but for some reason it made me think of the solution.
What I really want form to be is a dynamic type. If I change this line:
dynamic form = uiWrapper.GetForm();
Everything works :)
On top of that, logic.Save(form.ToDomainClass()) also goes directly to Save(DomainTypeOfForm) rather than Save(BaseDomainForm) so I can avoid the headache there as well. I knew that once I figured out the problem here I could apply the answer in my logic class as well.