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

  • SEARCH
  • Home
  • 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 7496771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:46:38+00:00 2026-05-29T18:46:38+00:00

I have a weird need in an ASP.NET MVC 3 application which blocks my

  • 0

I have a weird need in an ASP.NET MVC 3 application which blocks my current progress. Here is the case:

I have a little search engine for the products and I render this search engine on multiple pages. This SE makes a HTTP POST request to product controller’s search action. It fine till here.

Let’s assume that I am on home controller’s index action (/home/index). I make a search and check if ModelState.IsValid. As a result, it is not valid. So, I should return this back with the entered model (so that user won’t lose the values) and model state errors. But when I do that I ended up with different URL (/product/search) as expected.

If I do a redirect, I lose the ModelState and cannot display error messages.

I have different solutions so far and they all look dirty. Any idea?

Edit

Here is a little project which demonstrates this:

This is the ProductController:

public class ProductController : Controller {

    [HttpPost]
    public ActionResult Search(SearchModel searchModel) {

        if (ModelState.IsValid) { 
            //Do some stuff...

            return RedirectToAction("Index", "SearchResult");
        }
        return View(searchModel);
    }
}

This is the SearchModel:

public class SearchModel {

    [Required]
    public string ProductCategory { get; set; }

    [Required]
    public string ProductName { get; set; }
}

This is the *_SearchPartial*:

@model MvcApplication20.SearchModel

@using (Html.BeginForm("search", "product"))
{
    @Html.EditorForModel()

    <input type="submit" value="Search" />
}

And finally this is the Home controller Index action view which renders the *_SearchPartial*:

@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>

@Html.Partial("_SearchPartialView")

Here, when I submit the form and if the model state fails, how should I proceed at the Product controller Search action?

  • 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-05-29T18:46:40+00:00Added an answer on May 29, 2026 at 6:46 pm

    Here, when I submit the form and if the model state fails, how should
    I proceed at the Product controller Search action?

    Normally in this case you should render the _SearchPartialView but not as a partial but as a full view with layout so that the user can fix his errors. No need to stay at Home/Index in this case:

    [HttpPost]
    public ActionResult Search(SearchModel searchModel) {
    
        if (ModelState.IsValid) { 
            //Do some stuff...
    
            return RedirectToAction("Index", "SearchResult");
        }
        // since we are returning a view instead of a partial view,
        // the _SearchPartialView template should be displayed with the layout
        return View("_SearchPartialView", searchModel);
    }
    

    And if you wanted to stay on the same page upon error you could use an AJAX call to perform the search. So you would AJAXify this search form and then in the success callback test the result of the Search action and based on it decide whether to refresh the partial in order to show the error or redirect to the results action using window.location.href:

    something along the lines of:

    $(document).on('submit', '#searchForm', function() {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(), 
            success: function(result) {
                if (result.redirectTo) {
                    // no validation errors we can redirect now:
                    window.location.href = result.redirectTo;
                } else {
                    // there were validation errors, refresh the partial to show them
                    $('#searchContainer').html(result);
    
                    // if you want to enable client side validation
                    // with jquery unobtrusive validate for this search form
                    // don't forget to call the .parse method here 
                    // since we are updating the DOM dynamically and we
                    // need to reattach client side validators to the new elements:
                    // $.validator.unobtrusive.parse(result);
                }
            }
        });
        return false;
    });
    

    This obviously assumes that you have now wrapped the partial call in a div with id="searchContainer" and that you provided an id="searchForm" when generating the search form:

    <div id="searchContainer">
        @Html.Partial("_SearchPartialView")
    </div>
    

    and now the search action:

    [HttpPost]
    public ActionResult Search(SearchModel searchModel) {
    
        if (ModelState.IsValid) { 
            //Do some stuff...
    
            return Json(new { redirectTo = Url.Action("Index", "SearchResult") });
        }
        return PartialView("_SearchPartialView", searchModel);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little weird behavior with my ASP.NET MVC 2 application. I'm using
I have pretty weird question. In my ASP.NET MVC application I defined 1 Timer
I have a weird problem in asp.NET when calculating how many hours we need
In my ASP.NET MVC application, I have a project that contains all the business
I have an asp.net mvc calendar application (using jquery ui datepicker) and i am
I have a weird case here at work. The customer(telecommunication firm) has a server
I'm getting a weird issue in IE 8. Here's what we got. Running Asp.net
Kind of a weird question, but. I need to have a list of strings
I have a binary application on windows (train timetable software) which contains a lot
I have a weird situation, client does't want their application to support multitasking so

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.