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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:55:10+00:00 2026-05-19T13:55:10+00:00

How can I create a form in ASP.NET MVC2, send the data to a

  • 0

How can I create a form in ASP.NET MVC2, send the data to a controller that adds something to the database and then redirects to the home page? Can you give me an example/snippet of how it’s done in the View?


For some reason, I have a bug in my form. Here’s the code:

AddEvent.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Add Event</h2>
<% using (Html.BeginForm()) { %>

    <div>
        <%= Html.LabelFor(x => x.EventName) %>: 
        <%= Html.TextBoxFor(x => x.EventName) %>
    </div>
    <div>
        <%= Html.LabelFor(x => x.EventDate) %>: 
        <%= Html.TextBoxFor(x => x.EventDate) %>
    </div>
    <div>
        <%= Html.LabelFor(x => x.EventLocation) %>: 
        <%= Html.TextBoxFor(x => x.EventLocation) %>
    </div>
    <div>
        <%= Html.LabelFor(x => x.EventDescription) %>: </br> 
        <%= Html.TextAreaFor(x => x.EventDescription) %>
    </div>

    <input type="submit" value="Submit" />
<% } %>

HomeController.cs

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

    [HttpPost]
    public ActionResult AddEvent(Event e)
    {
        e.EventCreatorName = Session["UserName"].ToString();
        DatabaseModels db = new DatabaseModels();
        db.AddEvent(e);

        return RedirectToAction("Index", "Home");
    }

DatabaseModels.cs

    public bool AddEvent(Event e)
    {
        anEvent eventToAdd = new anEvent();
        eventToAdd.creator_nickname = e.EventCreatorName;
        eventToAdd.event_category = 1; // TODO
        if (e.EventDate == null)
        {
            eventToAdd.event_date = new DateTime();
        }
        else
        {
            eventToAdd.event_date = DateTime.Parse(e.EventDate);
        }
        eventToAdd.event_location = e.EventLocation;
        eventToAdd.event_name = e.EventName;

        m_db.AddToevents(eventToAdd);
        m_db.SaveChanges();
        return true;
    }

I type in details in the form and I get the following Exception:

This property cannot be set to a null value.

on event_location. Can anyone help solve this?

  • 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-19T13:55:11+00:00Added an answer on May 19, 2026 at 1:55 pm

    The asp.net/mvc site contains numerous examples, videos and tutorials about MVC that are worth reading. Here’s an example of how the scenario you are asking about could be implemented:

    Model:

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    

    Controller:

    public class PersonsController: Controller
    {
        public ActionResult Index()
        {
            return View(new Person());
        }
    
        [HttpPost]
        public ActionResult Index(Person person)
        {
            // The person object here will have it's FirstName
            // and LastName properties bound to whatever values
            // the user entered in the corresponding textboxes in the form
    
            // TODO: save person to database 
    
            // redirect to /home/index
            return RedirectToAction("index", "home");
        }
    }
    

    View:

    <%@ Page 
        Language="C#" 
        MasterPageFile="~/Views/Shared/Site.Master" 
        Inherits="System.Web.Mvc.ViewPage<AppName.Models.Person>" %>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <% using (Html.BeginForm()) { %>
            <div>
                <%= Html.LabelFor(x => x.FirstName) %>:
                <%= Html.TextBoxFor(x => x.FirstName) %>
            </div>
    
            <div>
                <%= Html.LabelFor(x => x.LastName) %>:
                <%= Html.TextBoxFor(x => x.LastName) %>
            </div>
    
            <input type="submit" value="Save" />
        <% } %>
    </asp:Content>
    

    Now you might be wondering about the TODO part. Usually I create a repository to decouple my data access logic from my controller:

    public interface IPersonsRepository
    {
        void Save(Person person);
    }
    

    and then use constructor injection of this repository into my controller:

    public class PersonsController: Controller
    {
        private readonly IPersonsRepository _repository;
        public PersonsController(IPersonsRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult Index()
        {
            return View(new Person());
        }
    
        [HttpPost]
        public ActionResult Index(Person person)
        {
            // The person object here will have it's FirstName
            // and LastName properties bound to whatever values
            // the user entered in the corresponding textboxes in the form
    
            // save person to database 
            _repository.Save(person);
    
            // redirect to /home/index
            return RedirectToAction("index", "home");
        }
    }
    

    Obviously now the last part that’s left is the implementation of this repository. This will depend on how/where your data is stored and the particular data access technology you would be using. So are you using a relational database, flat text file, XML file, object database, some database stored on the cloud, … how are you going to access it: EF, NHibernate, Linq-to-XML, some REST API, …

    Once you make your choice you simply implement the interface and instruct your DI framework to pass the proper implementation to the controller constructor.

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

Sidebar

Related Questions

Is there a good way to create a form in VB6 that can easily
I have a table in an ASP.NET MVC2 form. On each row in the
I am using ASP.NET MVC 2 Beta. I can create a wizard like workflow
I am using ASP.NET MVC2 and Entity Framework. I am going to simplify the
I'm using xval to use client side validation in my asp.net mvc2 web-application. Despite
I have a pretty big web application that I created last year using ASP.NET
I cannot access my UI controls on my ASP.Net website. When I create a
I can create a menu item in the Windows Explorer context menu by adding
I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
I can create a literal long by appending an L to the value; why

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.