1

I have a problem with validation of one view with multiple ViewModels. My situation is, that I have one Basic form, which is same for lot of pages. By ID parameter, I render new external fields to this Basic form. These external fields are type of ActionResult, using own ViewModel and own Controller. In Main controller on Post action I want to control if ModelState.IsValid, but I have problem - it validate all ViewModels of all external fields, but I want to validate only ViewModel of active external fields (and Basic form too).

It looks like this:

ViewModel of all view models

public class AllFieldsVm
{
    public BasicFormVm BasicFormVm { get; set; }
    public ExternalFieldXyVm ExternalFieldXyVm { get; set; }
    public AnotherExternalFieldVm AnotherExternalFieldVm { get; set; }
}

In controller of external fields I create new instance of AllFieldsVm and in this create new instance of ExternalFieldXyVm (if I need, I prefill these fields). This I render whitout layout like partial view (using @{Html.RenderAction("Action", "Controller", new {@someOptionalData = value});} ), when some condition is true.

In controller of Basic form on Post action I have something like this and I want to use something like this code if (ModelState.IsValid(model.BasicFormVm) && ModelState.IsValid(model.ExternalFieldXyVm)):

[POST("someurl-id{someId}")]
public ActionResult SaveFormData(int someId, AllFieldsVm model)
{
    //Here I want something like 
    //if (ModelState.IsValid(model.BasicFormVm) && ModelState.IsValid(model.ExternalFieldXyVm)) or something like that...

    var se = new SomeEntity();
    se.property1 = model.property1;
    se.property2 = model.property2;

    using (var dbc = _db.Database.BeginTransaction())
    {
        try
        {
            _db.Add(se);
            _db.SaveChanges();

            //My Condition - when save external data
            if (someId == (int) MovementTypes.SomeEnumInt)
            {
                var rd = new ExternalFieldEntity
                    {
                        PropertyA = se.property0,
                        PropertyB = Convert.ToDateTime(model.ExternalFieldXyVm.SomeExternalFieldName)
                    };

                _db.Add(rd);
                _db.SaveChanges();
            }

            dbc.Commit();
        }
        catch (Exception)
        {
            dbc.Rollback();
        }
    }

    return RedirectToAction("Action", "Controller");
}

So, my question is, how can I validate ExternalFieldXyVm separatly based on some conditions?

Is it possible, or I have to create all own validators, without using basic DataAnnotations or FluentValidation? I have no experience with these types of forms, so please be patient...

Thanks to all for help!!

3
  • Possible duplicate stackoverflow.com/questions/1135320/… Commented May 15, 2014 at 8:46
  • Simranjeet: no, it isn't duplicate. I know how to validate properties which I want in viewmodel. I want to control, which one ViewModel will be validated on controller. This desicion should be based on result of some condition. I need to know, if I can use ModelState.IsValid for concrete Vm of multiple Vms on page (and in one POST action of view in controller), or not, or how can I do this... Commented May 15, 2014 at 8:56
  • 1
    ModelState.IsValid wouldn't help. It will validate all rather than a particular one. Commented May 15, 2014 at 9:02

1 Answer 1

1

Great, I got it. I play with this for two days, don't know how it is possible that I didn't see that.

Result is: When view with own view model which is included in main viewmodel, isn't rendered into view, this viewmodel is not validate on post action. So my Basic form is validate everytime, and ExternalFields are validate only when are rendered. So sorry, for so stupid question....

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

2 Comments

Can you provide your code now that you don't use ModelState please? I'm having the same problem and I don't understand your answer.
In code I made some changes - on controller, when I want to save something which is in some viewmodel - if model.ViewModel1 != null and some other conditions or parameters, make this. If model.Viewmodel2 != null make this... For validation of these viewmodels I use FluentValidation, so validation process is made before calling post action on controller.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.