user1553604
user1553604

Reputation: 205

How to Update Database on ButtonClick in ASP.NET MVC architecture

Hi i am looking to capture values from my view and update the entered values in to my database .

My view looks like following :

@using Kendo.Mvc.UI 
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Facility</title>
</head>
<body>
    <div>
       <p>Create a new Facilty</p>
        <table width="100px">
        <tr>
        <td>
        <label for="TenantId">TenantID:</label>
        </td>
        <td>
        <input id="TextTenantId" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="FacilityID">FacilityID:</label>
        </td>
        <td>
        <input id="TextFacilityID" type="text" />
        </td>
        </tr>
         <tr>
        <td>
       <label for="FacilityGroupID">FacilityGroupID:</label>
        </td>
        <td>
        <input id="TextFacilityGroupID" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="FacilityName">FacilityName:</label>
        </td>
        <td>
        <input id="TextFacilityName" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="FacilityAddressLine1">FacilityAddressLine1:</label>
        </td>
        <td>
        <input id="TextFacilityAddressLine1" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="FacilityAddressLine2">FacilityAddressLine1:</label>
        </td>
        <td>
       <input id="TextFacilityAddressLine2" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="FacilityAddressLine3">FacilityAddressLine1:</label>
        </td>
        <td>
        <input id="TextFacilityAddressLine3" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="CityId">CityId:</label>
        </td>
        <td>
        <input id="TextCityId" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="StateId">StateId:</label>
        </td>
        <td>
        <input id="TextStateId" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="CountryId">CountryId:</label>
        </td>
        <td>
        <input id="TextCountryId" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="Zipcode">Zipcode:</label>
        </td>
        <td>
        <input id="TextZipcode" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="PhoneNo">PhoneNo:</label>
        </td>
        <td>
        <input id="TextPhoneNo" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="Status">Status:</label>
        </td>
        <td>
        <input id="TextStatus" type="text" />
        </td>
        </tr>
        <tr>
        <td>
       <label for="EmailId">EmailId:</label>
        </td>
        <td>
        <input id="TextEmailId" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <label for="Website">Website:</label>
        </td>
        <td>
        <input id="TextWebsite" type="text" />
        </td>
        </tr>
        <tr>
        <td>
        <input id="Submit1" type="Submit" value="submit" />
        </td>
        </tr>
        </table>
    </div>
</body>
</html>

My model looks like :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AmanoMockUp.Models
{
    public class FacilityModels
    {
        public int TenantId { get; set; }
        public int FacilityId { get; set; }
        public int FacilityGroupId { get; set; }
        public string FacilityName { get; set; }
        public string FacilityAddressLine1 { get; set; }
        public string FacilityAddressLine2 { get; set; }
        public string FacilityAddressLine3 { get; set; }
        public int CityId { get; set; }
        public int StateId { get; set; }
        public int CountryId { get; set; }
        public string Zipcode { get; set; }
        public int PhoneNo { get; set; }
        public bool status { get; set; }
        public string EmailId { get; set; }
        public string Website { get; set; }
        public DateTime CreationDate { get; set; }


    }
}

now i need to update the values entered in the view in the various textfields onto my database on the submission of my button

<input id="Submit1" type="Submit" value="submit" />

Where should i write the buttonclick event should it be on the controller or the model or should i create a whole new controller for the same .Please help !! I have searched but i couldnt find anything related to my issue . Thanx in advance !!

Upvotes: 0

Views: 2287

Answers (2)

JTMon
JTMon

Reputation: 3199

How about having your view strongly typed against your model and having the form submit the values to an action method that does the work? do some searching on Html.BeginForm for pointers on having a form call a specific action on submission and just about any basic MVC tutorial for strongly typing a view to a model.

Upvotes: 2

Mutu Yolbulan
Mutu Yolbulan

Reputation: 1052

database calls should be in the controller.

in the controller create another action with the [HttpPost] attribute but with the same name. it will take the model as its parameter. then complete the database logic and save changes and return the view.

look at the example here http://www.c-sharpcorner.com/UploadFile/krishnasarala/select-insert-update-and-delete-with-Asp-Net-mvc/

Upvotes: 0

Related Questions