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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:09:25+00:00 2026-05-26T17:09:25+00:00

so what Iam trying to do is a view wiht both create and list

  • 0

so what Iam trying to do is a view wiht both create and list in the same view, so I’ve read that I can usea ViewModel for this so I create my view model “EventoViewModel”

 public class EventoViewModel
    {
        public Eventos Eventos { get; set; }
        public IEnumerable<Eventos> LEventos { get; set; } 
    }

my controller:

 public ActionResult Create()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Create(Eventos eventos)
        {
            if (ModelState.IsValid)
            {
                db.Eventos.AddObject(eventos);
                db.SaveChanges();
            }
            return RedirectToAction("Create");
        }

My view: in the foreach part I get an ERROR, say nullreferenceException, what Iam doing wrong?

@model createList.Models.EventoViewModel

<h2>Create</h2>

@using (Html.BeginForm()) {
    <fieldset>

            <div class="editor-label">
            @Html.LabelFor(model => model.Eventos.Nombre)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Eventos.Nombre)               
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}


<table>
    <tr>
        <th>
            Nombre
        </th>
    </tr>

@foreach (var item in Model.LEventos) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Nombre)
        </td>
    </tr>
}

</table>
  • 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-26T17:09:26+00:00Added an answer on May 26, 2026 at 5:09 pm

    I change my approach I use partial view now. and create a “List” partial view

    Partial View: List

    @model IEnumerable<createList.Models.Eventos>
    
    <table>
        <tr>
            <th>
                Nombre
            </th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Nombre)
            </td>
        </tr>
    }
    </table>
    

    and in the end of my “create” view I call my “List” Partial view

    @model createList.Models.Eventos
    
    <h2>Create</h2>
    
    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Eventos</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Nombre)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Nombre)
                @Html.ValidationMessageFor(model => model.Nombre)
            </div>
    
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
    **@{Html.RenderAction("list", "Evento");}** // call de action "list"
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
    

    and my controllers:

    public ViewResult List()
            {
                return View(db.Eventos.ToList()); //past a list of eventos
            }
    
    
     public ActionResult Create()
            {
                return View();
            } 
    
            [HttpPost]
            public ActionResult Create(Eventos eventos)
            {
                if (ModelState.IsValid)
                {
                    db.Eventos.AddObject(eventos);
                    db.SaveChanges();
                    return RedirectToAction("Create");  
                }
    
                return View(eventos);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam trying to create a list view dynamically, Please provide me an example for
I am trying create a 'live' view where our clients can edit the colors
I am trying to create a view that contains some buttons placed vertically and
I am trying to create a view for a UNION of 2 select statements
I am trying to write a view that will generate a report which displays
I am trying to add a view to a custom list. The target is
I am trying to save my data from a view that was loaded on
I am trying to generate a UITextField in a view like this. I don't
I am trying to view the entries into my sql database that were entered
I am trying to view a message box. this should happen when the user

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.