1

In my application I am using Entity Framework 6 and ASP.NET MVC in C#.

I have a table that has records that I plan on populating my Index page with. How do I populate the index page without having the system add the id of the record to the URL. See example below. I have already looked at routing but with adding custom route you are forced to add more text to the url when all I want is the URL to show up as example.com. I don't want and don't need example.com/MenuRecords/Details/20 for a user to see.

So example.com should load the following data from the model below in the index view of the HomeController.

index.cshtml page calling the model data shown below:

@model example.Models.tblMenuRecords

@Model.ThisWeeksBestDrink
@Model.ThisWeeksBestAppetizer
@Model.ThisWeeksBestDesert
@Model.ThisWeeksBestLunchSpecial

This is the cntroller action method:

public ActionResult Index()
{
    return View();
}

How do I get that to work properly for the Index page? Since this is the home page that is calling data from a model I cannot have the URL have anything other than example.com .... but I do understand that when calling data from a model you do need some sort of ID but I just do not really understand how to do that.

I know that there is the route config that includes this default route that allows you to show only the name of the domain...But how is this done when you are trying to load data from the database.

routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }

Is this the correct way to pass an instance of the tblMenuRecords to the view?

public ActionResult Index()
{
    tblMenuRecords tblMenuRecords = db.tblMenuRecords();

    return View(tblMenuRecords);
}
6
  • You don't need an ID in a URL to shove data into a model object and pass it to a view. Try it out. Pass an instance of tblMenuRecords to your view. Commented Sep 17, 2021 at 1:45
  • Yeah thats what I am trying to accomplish. I was trying to pass an instance of the tblMenuRecords to the index view like I showed above, but as soon as I call the @Model.ThisWeeksBestLunchSpecial or any other field in that table I get the error that says Not Set Instance of an object, the error is pointing at that call for the fields and the page wont load unless the url has the id of the record. Commented Sep 17, 2021 at 2:07
  • You haven't shown passing an instance of your tblMenuRecords into the view from your action method. Show that. Commented Sep 17, 2021 at 2:09
  • Oh you mean like this? public ActionResult Index() { tblMenuRecords tblMenuRecords = db.tblMenuRecords(); return View(tblMenuRecords); } Commented Sep 17, 2021 at 2:17
  • Yes. If your view expects a model instance, then you should pass a one to it from the action. You're just invoking the constructor, so the models properties are probably empty. Do you store those objects in a database? Then retrieve it from there. Commented Sep 17, 2021 at 12:17

2 Answers 2

0

I think you have to fix the action

public ActionResult Index()
{
    tblMenuRecords tblMenuRecords = db.tblMenuRecords.FirstOrDefault();

    return View(tblMenuRecords);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Serge. This implementation worked for me. I never thought to use FirstOrDefault, that did help alot .
You are welcome!
0
        I think your view is missed with model. View code should be like below
        @model Models.MenuRecords
        @{
            ViewBag.Title = "Menu Records";
        }
        
        <h2>Details</h2>
        
        <div>
            <h4>Menus</h4>
        <hr />
        <table>
            <tr>
                <td>
                    @Html.DisplayFor(model => model.ThisWeeksBestDrink)
                </td>
                 <td>
     @Html.DisplayFor(model => model.ThisWeeksBestLunchSpecial)
       </td>
            </tr>
        </table>
        </div>
I hope, it will help you. 

Comments

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.