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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:16:24+00:00 2026-06-13T09:16:24+00:00

I am new to MVC3 and trying to work on my first small project

  • 0

I am new to MVC3 and trying to work on my first small project at work. I have a HomeController with two actions, Index() and GetData(). Index just returns the Index view. When the Index page loads, I want there to be an html Button or Input of type Button. When I click on this button, I want the GetData action to run, which selects some data based on a linq query and returns a List. Then in the Index view itself, below the button which i clicked, I want the list to be populated onto an html table …..I assume using a foreach loop.

This is not working, and I think I am clearly misunderstanding the concept of this MVC pattern.

My Controller Code:

    private ReferenceDataContext data = new ReferenceDataContext();

    public ActionResult Index()
    {

        return View();

    }

    public ActionResult GetData()
    {

        using (data)
        {                
            var hosp = (from x in data.Hospitals
                          where x.HAcode == "Q36"
                          select x).ToList();
            ViewData["hosp"] = hosp;
            return RedirectToAction("Index");                
        }

    } 

My View Code:

@model IEnumerable<NursingHomes.Models.Hospitals>

@{
    ViewBag.Title = "Hospitals Verify";
}


<br />
<br />
@using (Html.BeginForm("GetData", "Home")) 
{ 
<input type="submit" value="Submit" title="Get Data" />  

<h2>Nursing Homes</h2>

<table>
<tr>
<th>Code</th>
<th>HAcode</th>
<th>Name</th>

</tr>

@foreach (NursingHomes.Models.Hospitals item in (List<NursingHomes.Models.Hospitals>)    ViewData["hosp"])
{ 
<tr>
<td>
@Html.DisplayFor(modelItem=>item.Code)
</td>
<td>
@Html.DisplayFor(modelItem=>item.HAcode)
</td>
<td>
@Html.DisplayFor(modelItem=>item.Name)
</td>

</tr>

}

</table>

} 

I am doing something wrong above? Is is this simply not possible without the use of JQuery?

Basic question is, how can click on that button on the Index page, and tell it to run the GetData action and then build the table below the button using the returned List from the GetData 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-06-13T09:16:25+00:00Added an answer on June 13, 2026 at 9:16 am

    Try returning View from Get Data Action as below:

    return View("Index",YourModelObject);
    

    and you can use this Model in view as Below:

    ...
    <table>
    <tr>
    <th>Code</th>
    <th>HAcode</th>
    <th>Name</th>
    
    </tr>
    If(Model !=null)
    {
    @foreach (NursingHomes.Models.Hospitals item in Model)
    { 
    <tr>
    <td>
     .....
    

    EDIT: If you want keep URL as Same then use index action for POST action also as below:

    public ActionResult Index()
            {
                if (Request.HttpMethod.ToLower().Equals("get"))
                {
                    return View();
                }
                else
                {
                    using (data)
                    {
                        var hosp = (from x in data.Hospitals
                                    where x.HAcode == "Q36"
                                    select x).ToList();
    
                        return View(hosp);
                    }
                }
            }
    

    In your View you should remove the action name and controller name params.

    @using (Html.BeginForm("GetData", "Home")) 
    { 
    <input type="submit" value="Submit" title="Get Data" /> 
    .....
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to mvc3. I created one web project though mvc3 . On Index
I am new to ASP.NET MVC3 and trying desperately to just get something simple
I'm new to MVC3 and have been working on a small site using EF
I trying to do something like that First, Create a new mvc 3 project
New to MVC3 Razor - linq to sql having spent some time trying to
I am using MS Visual Studio 2010. When i create a new MVC3 project
I am new in MVC3 i am going to create a MVC3 test project
I'm somewhat new to MVC3, so I have a few questions... I am creating
I am trying to deploy my first MVC3 application to a server running IIS-7.5.
I'm starting a new web project using ASP.NET Webforms + EF4. I'm trying to

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.