2

I've been trying to get my head around how to solve this problem in the cleanest way.

How to get the model binder to NOT create empty objects when submitting empty strings for complex types?

Say I've got this model:

public class CreateCaseModel
{
    [Required]
    public UserModel Contact { get; set; }

    public UserModel Assigned {get; set; }
}

and the user-model:

public class UserModel {
    [Required]
    public string Id {get;set;}

    public string Name {get;set;
}

And in the view i have these inputs:

<input type="hidden" name="Assigned.Id" value="" />
<input type="hidden" name="Contact.Id" value="userId1" />

I post this to a controller something like:

public ActionResult Create(CreateCaseModel model){
    if (!ModelState.IsValid)
    {
        //handle error       
    }else {
        //create
    }
}

In the controller now my model.Contact and model.Assigned will never be null. Only the model.Contact will however contain anything (the Idwill be set) At this point the modelstate will also be invalid due to the fact that the UserModel.Id field is marked as required.

What I want to happen is for the binder to not create the UserModel object at all if the input is empty.

The entire reason for me needing this is that I'm using the UserModel.Name field elsewhere in the view, and I'd like to group the two fields together. And I want to use the ModelState.IsValidcheck for serverside validation.

I could of course go with extracting the Id and Name fields of each userobjects and put them directly in the CreateCaseModel, but I want to explore if what I describe is even possible.

Grateful for any tips or pointers!

4
  • 1
    Why are you manually creating html input element and hoping you type the correct names instead of using the HtmlHelper methods? (because your names are incorrect). Commented Aug 28, 2014 at 15:35
  • It sounds like after a POST request, inside your CreateCaseModel that if a non-required field (Name) is empty that you do not want it to create an empty string? Or do you not want it to create an empty object at all if the name is missing? So say a user submits only a "Contact" and not an "Assigned" you don't want "Assigned" to be created? Commented Aug 28, 2014 at 15:35
  • Erik Phillips: I'm not manually creating the input fields. I'm actually using a Select2 dropdown to populate the input field in the end, I just used this as an example. Commented Aug 28, 2014 at 15:41
  • code4coffee: Whenever the Id is submitted blank I don't want the object to be created at all. I want it to be null. The Name property is only used when rendering the view in the first place. Commented Aug 28, 2014 at 15:44

1 Answer 1

2

How to get the model binder to NOT create empty objects when submitting empty strings for complex types?

Write your own custom ModelBinder. The default model binder will always attempt to create complex types for recursive modelbinding.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, this is kind of what I was afraid of. Kind of the point I'm trying to make is, am I attacking this problem in the wrong way? Is the concept of grouping my parameters together in a new model-object way off?
@aeliusd just stop caring. validate the objects as necessary. Only despair awaits you if you create your own model binder.
heh, yeah I can imagine :). I'm at the moment more leaning towards not using complex types but going the route of using a flat model with alot of strings instead...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.