Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8984053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:59:42+00:00 2026-06-15T20:59:42+00:00

I have a form in MVC 4 which contains several fields and, depending on

  • 0

I have a form in MVC 4 which contains several fields and, depending on the value of a combo, I need to open a modal dialog form and load into that one 3 additional fields that will impact against the same entity that I’m creating/editing in the main form.
For this modal dialog I’m using the one from jQuery UI.

Now, what I need to do is to validate (Required) the fields within the modal dialog in order to allow the user to retain the entered values which will be submited later by the main form.

My problem is how to perform the validation of those 3 fields from within the modal form (because they wouldn’t be able to submit the main form until dialog is closed).

Any hints or ideas?

Regards,
Cesar.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:59:43+00:00Added an answer on June 15, 2026 at 8:59 pm

    You could use AJAX to submit the form modal to the server. The modal form will have of course a separate view model associated with it. Let’s exemplify:

    Main view model:

    public class MyViewModel
    {
        [DisplayName("select a value")]
        public string SelectedValue { get; set; }
        public IEnumerable<SelectListItem> Values { get; set; }
        public string SomeOtherProperty { get; set; }
    }
    

    Modal dialog view model:

    public class DialogViewModel
    {
        [Required]
        public string Prop1 { get; set; }
        [Required]
        public string Prop2 { get; set; }
        [Required]
        public string Prop3 { get; set; }
    }
    

    Then you could have a controller containing 4 actions:

    public class HomeController : Controller
    {
        // Renders the main form
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Values = new[]
                {
                    new SelectListItem { Value = "1", Text = "item 1" },
                    new SelectListItem { Value = "2", Text = "item 2" },
                    new SelectListItem { Value = "3", Text = "item 3" },
                }
            };
            return View(model);
        }
    
        // Processes the submission of the main form
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return Content(
                string.Format(
                    "Thanks for filling out the form. You selected value: \"{0}\" and other property: \"{1}\"",
                    model.SelectedValue,
                    model.SomeOtherProperty
                )
            );
        }
    
        // Renders the partial view which will be shown in a modal
        public ActionResult Modal(string selectedValue)
        {
            var model = new DialogViewModel
            {
                Prop1 = selectedValue
            };
            return PartialView(model);
        }
    
        // Processes the submission of the modal
        [HttpPost]
        public ActionResult Modal(DialogViewModel model)
        {
            if (ModelState.IsValid)
            {
                // validation of the modal view model succeeded =>
                // we return a JSON result containing some precalculated value
                return Json(new
                {
                    value = string.Format("{0} - {1} - {2}", model.Prop1, model.Prop2, model.Prop3)
                });
            }
    
            // Validation failed => we need to redisplay the modal form
            // and give the user the possibility to fix his errors
            return PartialView(model);
        }
    }
    

    Next you could have a main view (~/Views/Home/Index.cshtml):

    @model MyViewModel
    
    @using (Html.BeginForm())
    {
        <div>
            @Html.LabelFor(x => x.SelectedValue)
            @Html.DropDownListFor(x => x.SelectedValue, Model.Values, new { id = "ddl" })
        </div>
        <div>
            @Html.LabelFor(x => x.SomeOtherProperty)
            @Html.TextBoxFor(x => x.SomeOtherProperty, new { id = "otherProperty" })
            @Html.ActionLink(
                "click here to open a modal and help you fill the value",
                "Modal",
                "Home",
                null,
                new { id = "showModal" }
            )
        </div>
        <button type="submit">OK</button>
    }
    
    <div id="modal"></div>
    

    and a partial view to contain the modal form (~/Views/Home/Modal.cshtml):

    @model DialogViewModel
    
    @using (Ajax.BeginForm(new AjaxOptions { OnSuccess = "handleModalSubmit" }))
    {
        <div>
            @Html.LabelFor(x => x.Prop1)
            @Html.EditorFor(x => x.Prop1)
            @Html.ValidationMessageFor(x => x.Prop1)
        </div>
        <div>
            @Html.LabelFor(x => x.Prop2)
            @Html.EditorFor(x => x.Prop2)
            @Html.ValidationMessageFor(x => x.Prop2)
        </div>
        <div>
            @Html.LabelFor(x => x.Prop3)
            @Html.EditorFor(x => x.Prop3)
            @Html.ValidationMessageFor(x => x.Prop3)
        </div>
        <button type="submit">OK</button>
    }
    

    OK, now all that’s left is write some javascript to make the whole thing alive. We start by making sure that we have included all the required scripts first:

    <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    

    and then write our own:

    $(function () {
        $('#showModal').click(function () {
            $.ajax({
                url: this.href,
                type: 'GET',
                cache: false,
                data: { selectedValue: $('#ddl').val() },
                success: function (result) {
                    $('#modal').html(result).dialog('open');
                }
            });
            return false;
        });
    
        $('#modal').dialog({
            autoOpen: false,
            modal: true
        });
    });
    
    function handleModalSubmit(result) {
        if (result.value) {
            // JSON returned => validation succeeded => 
            // close the modal and update some property on the main form
            $('#modal').dialog('close');
            $('#otherProperty').val(result.value);
        } else {
            // validation failed => refresh the modal to display the errors
            $('#modal').html(result);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my mvc 3 project I have a form which contains a list of
I have a form in an MVC view which contains a number of text
I have an ajax form in asp.net mvc which is as simple as this:
I have a simple MVC form with the following elements: <%= Html.TextBox(FechaInicio) %> Which
I am using ASP.NET MVC 1.0. I have an ActionResult which receives a form
I have two buttons on my MVC form: <input name=submit type=submit id=submit value=Save />
I am using ASP.Net MVC. I have a partial view which has a form
I have a form on an ASP.NET MVC site which I've recently changed from
I have a form which contains a whole bunch of checkboxes and some other
I have a simple ASP.Net MVC View which contains an FCKeditor text box (created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.