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 3699652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:07:44+00:00 2026-05-19T05:07:44+00:00

Inside my controller’s action I have the following code: public ActionResult GridAction(string id) {

  • 0

Inside my controller’s action I have the following code:

public ActionResult GridAction(string id)
{
    if (String.IsNullOrEmpty(id)) 
    {
        // add errors to the errors collection and then return the view saying that you cannot select the dropdownlist value with the "Please Select" option
    }

    return View(); 
}

UPDATE:

if (String.IsNullOrEmpty(id))
{
    // add error 
    ModelState.AddModelError("GridActionDropDownList", "Please select an option");
    return RedirectToAction("Orders"); 
}

UPDATE 2:

Here is my updated code:

@Html.DropDownListFor(x => x.SelectedGridAction, Model.GridActions,"Please Select") 
@Html.ValidationMessageFor(x => x.SelectedGridAction)  

The Model looks like the following:

public class MyInvoicesViewModel
{

    private List<SelectListItem> _gridActions;

    public int CurrentGridAction { get; set; }

    [Required(ErrorMessage = "Please select an option")]
    public string SelectedGridAction { get; set; }

    public List<SelectListItem> GridActions
    {
        get
        {
            _gridActions = new List<SelectListItem>();
            _gridActions.Add(new SelectListItem() { Text = "Export to Excel", Value = "1" });

            return _gridActions;
        }
    }
} 

And here is my controller action:

public ActionResult GridAction(string id)
{
    if (String.IsNullOrEmpty(id))
    {
        // add error 
        ModelState.AddModelError("SelectedGridAction", "Please select an option");
        return RedirectToAction("Orders"); 
    }

    return View(); 
}

Nothing happens! I am totally lost on this one!

UPDATE 3:

I am now using the following code but still the validation is not firing:

public ActionResult GridAction(string id)
{
    var myViewModel= new MyViewModel();
    myViewModel.SelectedGridAction = id; // id is passed as null           

    if (!ModelState.IsValid)
    {
        return View("Orders");
    }

UPDATE 4:

$("#linkGridAction").click(function () {
    alert('link grid action clicked'); 

    $.get('GridAction/', { SelectedGridAction: $("#SelectedGridAction").val() }, function (result) {
        alert('success');
    });
});

And the Controller looks like the following:

// OrderViewModel has a property called SelectedGridAction. 
public ActionResult GridAction(OrderViewModel orderViewModel)
{
    return View(); 
}

UPDATE 5: Validation is not firing:

public ActionResult GridAction(OrderViewModel orderViewModel)
{
    if (!ModelState.IsValid)
    {
        return View("Orders", orderViewModel); 
    }
    return View(); 
}
  • 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-19T05:07:44+00:00Added an answer on May 19, 2026 at 5:07 am

    You could use a view model:

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

    and then:

    public ActionResult GridAction(MyViewModel model)
    {
        if (ModelState.IsValid)
        {
            // the model is valid, the user has selected an id => use it
            return RedirectToAction("Success");
        }
        return View();
    }
    

    UPDATE:

    After the hundreds of comments on my answer I feel in the necessity to provide a full working example:

    As usual start with a view model:

    public class MyViewModel
    {
        [Required]
        public string SelectedItemId { get; set; }
    
        public IEnumerable<SelectListItem> Items 
        {
            get
            {
                // Dummy data
                return new SelectList(Enumerable.Range(1, 10)
                    .Select(i => new SelectListItem 
                    {
                        Value = i.ToString(),
                        Text = "item " + i 
                    }), 
                "Value", "Text");
            }
        }
    }
    

    Then a controller:

    public class HomeController: Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            if (!ModelState.IsValid)
            {
                // The user didn't select any value => redisplay the form
                return View(model);
            }
            // TODO: do something with model.SelectedItemId
            return RedirectToAction("Success");
        }
    }
    

    and finally the view:

    <% using (Html.BeginForm()) { %>
        <%= Html.DropDownListFor(
            x => x.SelectedItemId, 
            Model.Items, 
            "-- Select Item --"
        ) %>
        <%= Html.ValidationMessageFor(x => x.SelectedItemId) %>
        <input type="submit" value="OK" />
    <% } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex action inside controller that performs several update queries to the
Inside my page, I have the following: <aspe:UpdatePanel runat=server ID=updatePanel> <ContentTemplate> <local:KeywordSelector runat=server ID=ksKeywords
I have a Tab Bar controller inside a Navigation controller. I want to create
I have a View Controller inside my MainWindow.xib file that loads the nib Cover
I need to be able to quickly convert an image (inside a rails controller)
Inside my code I'm generating hashes of URLs, (which are practically of unbounded length).
I have table inside a div tab. The table has 40 rows in it
I am inside of... public class bgchange : IMapServerDropDownBoxAction { void IServerAction.ServerAction(ToolbarItemInfo info) {
I've some annotation-based controller inside a sub module. These modules are deployed as jar
I was wondering why someone should use helper_method inside a controller to create a

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.