1

I've been looking around and for the life of me I can't find an example that shows how to do this. They either have examples where they just load data from external file or enter data manually into the table. I have a simple form with two boxes and submit button that uploads data when user clicks submit to a SQL table that I created. I know this sounds simple but I can't find a way to do it. I'm completely new to MVC4, I've read ton of material and done most of tutorials and I understand the concept behind it. I just can't figure out syntax for doing this.

If anyone can walk me through this I would be EXTREMELY grateful. I'm using MVC4 to develop this.

Basically this is what I need done:

  • Text box for Event Name (created)
  • Text box for Event Data (created)
  • Submit Button (created)

  • SQL Table with same properties (created using ADO Entity Data Model)

  • Import statement (????)

2 Answers 2

2

You could start by creating your EF model:

public class Event
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string EventName { get; set; }
    public string EventData { get; set; }
}

and a corresponding data context:

public class EventsDataContext: DbContext
{
    public IDbSet<Event> Events { get; set; }
}

a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new Event());
    }

    [HttpPost]
    public ActionResult Index(Event model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        using (var ctx = new EventsDataContext())
        {
            ctx.Events.Add(model);
            ctx.SaveChanges();
        }

        return Content("The new event was successfully stored into the database");
    }
}

and finally a view:

@model Event

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.EventName)
        @Html.EditorFor(x => x.EventName)
        @Html.ValidationMessageFor(x => x.EventName)
    </div>

    <div>
        @Html.LabelFor(x => x.EventData)
        @Html.EditorFor(x => x.EventData)
        @Html.ValidationMessageFor(x => x.EventData)
    </div>

    <button type="submit">Create event</button>
}

Further reading: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4

And yet another tutorial: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Sign up to request clarification or add additional context in comments.

8 Comments

I'm getting error: Error 2 'System.Linq.IQueryable<MvcApplication1.Models.Event>' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Linq.IQueryable<MvcApplication1.Models.Event>' could be found (are you missing a using directive or an assembly reference?) C:\Users\n00151956\Desktop\TestMVCProject\PartyInvites\Invitations\MvcApplication1\Controllers\HomeController.cs 30 28 Invitations
The error is for the Add method in the Home Controller. Any idea why it can't be recognized? Am I missing some import/using statement?
Sorry I made a mistake in my answer. You should use IDbSet<Event> instead of IQueryable<Event>.
No problem. That seemed to work and it comes back with "The new event was successfully stored into the database" but its not actually inserted into my SQL Table. Where did the data actually go?
This will depend on which connection string you configured in your web.config.
|
0

Using visual studio:

Choose New project, and select "MVC4 project", then "Basic"

Once your new project has opened, right click the Models folder and choose Add Class... Call your class EventModel and add the following code:

public class EventModel
{
    [Key]
    public int EventId { get; set; }
    public string EventName { get; set; }
    public string EventData { get; set; }
}

Now compile your project.

Then right click on controllers and choose add controller. Give it a name of 'EventController'

For template choose 'MVC Controller with read/write actions and views, using entity framework'

For Model class choose 'EventModel'

For datacontext class choose 'New data context'.

Job done. Visual studio should have created views to list, create, delete, show and edit Events

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.