Showing posts with label aspnet mvc Create page with Entity Framework. Show all posts
Showing posts with label aspnet mvc Create page with Entity Framework. Show all posts

Sunday, 26 March 2023

06. aspnet mvc Create page with Entity Framework | Asp.Net MVC create re...


Asp.Net MVC CRUD 

Controller Code:  FreindController.cs


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

namespace MvcTeach.Controllers
{
    public class FriendController : Controller
    {
        dbMbkTestEntities db = new dbMbkTestEntities();
        // GET: Friend
        public ActionResult Index()
        {
            var FrndList = (from a in db.tblFriends select a).ToList();
            return View(FrndList);
        }


        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(tblFriend frnd)
        {
            tblFriend newFrnd = new tblFriend();
            newFrnd.FriendName = frnd.FriendName;
            newFrnd.City = frnd.City;
            newFrnd.Mobile = frnd.Mobile;
            db.tblFriends.Add(frnd);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}



@model MvcTeach.tblFriend

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>tblFriend</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @*<div class="form-group">
            @Html.LabelFor(model => model.FriendID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FriendID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FriendID, "", new { @class = "text-danger" })
            </div>
        </div>*@

        <div class="form-group">
            @Html.LabelFor(model => model.FriendName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FriendName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FriendName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>