-2

I have a little different view of the common views in my project. It's using Ajax.StartForm like the all others. But in this case I just want to send a JavaScript object parsed in JSON, and not the value of the fields of my form.

To illustrate, I have imagine this steps: On this form, when submit button was pressed, the default submit event would be intercepted and a JavaScript object would be created - gathering values from some custom widgets - to be sent to the Action. Then that object would be sent like data: JSON.stringify(that_object) like on jQuery.ajax.

The problem is that I don't want to use jQuery.ajax, I want to use MVC's Ajax.StartForm because of some functions and validations automatically activated by my application to it.

There's a way accomplish that behaviour?

If that was not possible, I'm afraid I will be forced to store that object parsed in JSON as a string in a hidden field in order to submit it.

UPDATE:

This is my ViewModel:

public class ProcessWizardViewModel
{
    public long ProcessId { get; set; }
    public List<long> ProcessIds { get; set; }
    public List<ProcessItemWizardViewModel> Items { get; set; }

    public ProcessWizardViewModel()
    {
        ProcessIds = new List<long>();
        Items = new List<ProcessItemWizardViewModel>();
    }
}

public class ProcessItemWizardViewModel
{
    public long Type { get; set; }
    public bool Dependence { get; set; }
    public long ProcessCultureId { get; set; }
    public ProcessItemWizardViewModel() { }
}

ProcessIds comes from a grid with checkbox selection and Items comes from a tree view with checkbox selection.

0

1 Answer 1

1

If you're posting to an MVC ActionMethod (of some sort), it makes more sense to just have it post as you normally would and let the model binder handle it. Otherwise, as far as I know, you're only other option would be using jQuery.ajax()

Updated from question updates

If your controller has an action that has a signature like this:

[HttpPost]
public ActionResult(ProcessWizardViewModel model) { }

and your view has @model ProcessWizardViewModel the model binder will handle the values correctly.

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

4 Comments

Yes, it will but I can't. It's a view with two widgets and I have to combine their data in just one object in order to MVC to serialize into a View Model.
Can you post the controller and view model(s)? There are other ways to handle it.
Just updated the question with the View Model. I don't know if the Controller is important here, there's anothing on it. Actually its receiving the object as a string and deserializing it.
The problem is that I want to get rid of that hidden, not just treat it with a binder. But I see that I can't do this. I have no hope anymore.. haha but your tip is nice, thank you anyway, dude.

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.