ASP.NET MVC database table getting null value - asp.net-mvc

I am creating a simple CRUD application using ASP.NET MVC with Entity Framework, Data are being saved but the values are null when I check it from Database ..
Below I shared different class and files:-
EmployeeController class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASP.NETMVCCRUD.Models;
using System.Data.Entity.Validation;
namespace ASP.NETMVCCRUD.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
using (DBModel db = new DBModel())
{
List<Employee> emplist = db.Employees.ToList<Employee>();
return Json(new { data = emplist }, JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
public ActionResult AddOrEdit(int id=0) {
return View(new Employee());
}
[HttpPost]
public ActionResult AddOrEdit(Employee emp)
{
using (DBModel db = new DBModel())
{
db.Employees.Add(emp);
db.SaveChanges();
return Json(new {success = true, message="Saved Successfully",JsonRequestBehavior.AllowGet });
}
}
}
}
AddOrEdit.cshtml
#model ASP.NETMVCCRUD.Models.Employee
#{
Layout = null;
}
#using (Html.BeginForm("AddOrEdit", "Employee", FormMethod.Post, new {onsubmit="return SubmitForm(this)" }))
{
#Html.HiddenFor(Model => Model.EmployeeID)
<div class="form-group">
#Html.HiddenFor(Model => Model.Name, new { #class = "control-label" })
#Html.EditorFor(Model => Model.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(Model => Model.Name)
</div>
<div class="form-group">
#Html.HiddenFor(Model => Model.Position, new { #class = "control-label" })
#Html.EditorFor(Model => Model.Position, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(Model => Model.Position)
</div>
<div class="form-group">
#Html.HiddenFor(Model => Model.Office, new { #class = "control-label" })
#Html.EditorFor(Model => Model.Office, new { htmlAttributes = new { #class = "form-control" } })
</div>
<div class="form-group">
#Html.HiddenFor(Model => Model.Age, new { #class = "control-label" })
#Html.EditorFor(Model => Model.Age, new { htmlAttributes = new { #class = "form-control" } })
</div>
<div class="form-group">
#Html.HiddenFor(Model => Model.Salary, new { #class = "control-label" })
<div class="input-group">
<span class="input-group-addon">$</span>
#Html.EditorFor(Model => Model.Salary, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary"/>
<input type="reset" value="Reset" class="btn " />
</div>
}
Employee.cs
namespace ASP.NETMVCCRUD.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public string Office { get; set; }
public Nullable<int> Age { get; set; }
public Nullable<int> Salary { get; set; }
}
}

Your view contains a #Html.HiddenFor() for each property before the associated EditorFor() method. The DefaultModelBinder only binds the first matching name/value pair and ignores the others, so its the values of the hidden inputs (which are default values) that are being saved.
Remove all the #Html.HiddenFor() from your view and the edited values will be correctly bound.
As a side note, its unclear why your method is named AddOrEdit when all you are doing is adding new records.

[HttpPost]
public ActionResult AddOrEdit(Employee emp)
{
using (DBModel db = new DBModel())
{
db.Employees.Add(emp);
try
{
db.SaveChanges();
}
catch(DbEntityValidationException e)
{
Console.WriteLine(e);
}
return Json(new { success = true, message = "Saved Succesfully" }, JsonRequestBehavior.AllowGet);
}
}

Related

how i can send multivalue to create action

i have a doctor i want add doctor subspecialty to the doctor from sub specialties table many to many relationship
i need to add subspecialties from multiselect list but my controller only add first selection , i want my create controller take all passed subspecialties and create it
my model
public partial class DoctorSubSpecialty
{
public int Id { get; set; }
public Nullable<int> DoctorId { get; set; }
public Nullable<int> SubSpecialtyId { get; set; }
public virtual DoctorProfile DoctorProfile { get; set; }
public virtual SubSpecialty SubSpecialty { get; set; }
}
}
create get action
public ActionResult Create()
{
ViewBag.DoctorId = new SelectList(db.DoctorProfiles, "Id", "FullName");
ViewBag.SubSpecialtyId = new MultiSelectList(db.SubSpecialties, "id", "Name");
return View();
}
create post action
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,DoctorId,SubSpecialtyId")] DoctorSubSpecialty doctorSubSpecialty)
{
DoctorSubSpecialty doctorSub = db.DoctorSubSpecialties.Where(d => d.DoctorId == doctorSubSpecialty.DoctorId & d.SubSpecialtyId == doctorSubSpecialty.SubSpecialtyId).FirstOrDefault();
if (doctorSub == null) {
db.DoctorSubSpecialties.Add(doctorSubSpecialty);
await db.SaveChangesAsync();
}
my view
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DoctorSubSpecialty</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.DoctorId, "DoctorId", htmlAttributes: new { #class = "control-label col-md-2", #id = "DoctorID" })
<div class="col-md-10">
#Html.DropDownList("DoctorId", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.DoctorId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SubSpecialtyId, "SubSpecialtyId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("SubSpecialtyId",(MultiSelectList)ViewBag.SubSpecialtyId, htmlAttributes: new { #multiple = "multiple", #class = "form-control" })
#Html.ValidationMessageFor(model => model.SubSpecialtyId, "", 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>
}
Create a ViewModel specific to your usecase that can actually transport more than one Id.
I.e. you will need an int[] to bind the selection to.
A ViewModel also helps you to get rid of all this ViewBag and [Bind] nonsense.
public class CreateDoctorSubSpecialtyViewModel {
// These are the selected values to be posted back
public int DoctorId { get; set; }
public int[] SubSpecialtyIds { get; set; }
// These are the possible values for the dropdowns
public IEnumerable<SelectListItem> DoctorProfiles { get; set; }
public IEnumerable<SelectListItem> SubSpecialties { get; set; }
}
GET action - initialize the ViewModel and pass it to the View:
[HttpGet]
public ActionResult Create() {
var doctorProfiles = db.DoctorProfiles.Select(d =>
new SelectListItem {
Text = d.FullName,
Value = d.Id
}
).ToArray();
var subSpecialties = db.SubSpecialties.Select(s =>
new SelectListItem {
Text = s.Name,
Value = s.id
}
).ToArray();
var viewModel = new CreateDoctorSubSpecialtyViewModel {
DoctorProfiles = doctorProfiles,
SubSpecialties = subSpecialties
};
return View("Create", viewModel);
}
View "Create.cshtml" (styling removed for clarity) - tell MVC which ViewModel we want to use with #model:
#model CreateDoctorSubSpecialtyViewModel
#using (Html.BeginForm("Create", "YourControllerName", FormMethod.Post)) {
#Html.DropDownListFor(m => m.DoctorId, Model.DoctorProfiles)
#Html.DropDownListFor(m => m.SubSpecialtyIds, Model.SubSpecialties, new { multiple = "multiple" })
<input type="submit" />
}
POST action - use Linq Contains to test against multiple submitted SubSpecialtyIds:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(CreateDoctorSubSpecialtyViewModel postData) {
DoctorSubSpecialty[] allSelectedSubSpecialities = db.DoctorSubSpecialties
.Where(d => d.DoctorId == postData.DoctorId
&& postData.SubSpecialtyIds.Contains(d.SubSpecialtyId))
.ToArray();
// ...
}
EDIT #Html.DropDownListFor requires an IEnumerable<SelectListItem> as second parameter.

How to Display Data in a single view from Multiple Tables in MVC using Entity framework

I want to display the the password and user name in the user view
could any one help me? I created a Userand LogIn table as shown below. How do I display properties from both the User and LogIn table in my Razor view.
Models
public class User
{
[Key]
public int UserID { get; set; }
public string Name { get; set; }
public string Mobile { get; set; }
public string JobTittle { get; set; }
public string Email { get; set; }
}
public class LogIn
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int LoginID { get; set; }
[Required(ErrorMessage = "Required.")]
public string UserName { get; set; }
public string Password { get; set; }
public User User { get; set; }
}
Controller
public class UsersController : Controller
{
private CodingContext db = new CodingContext();
// GET: Users
public ActionResult Index()
{
return View();
}
}
View
#model Coding.Models.User
#{
ViewBag.Title = "Create";
}
<h2>Sign Up</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Name, "", new { #class = "text-danger" })
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
The proper way to do this is to create a viewmodel that includes both the User and LogIn objects. But the quick and dirty method is to just use ViewBags.
public class UsersController : Controller
{
private CodingContext db = new CodingContext();
// GET: Users
public ActionResult Create()
{
ViewBag.User = db.Users.FirstOrDefault(x => x.UserID == someId);
ViewBag.LogIn = db.LogIns.FirstOrDefault(x => x.LoginID == someId);
return View();
}
}
You can then use those ViewBags in your view
<div>
ViewBag.User.UserID
ViewBag.LogIn.LoginID
</div>
I would advise you to never store passwords in plain text. They should be salted and hashed and never displayed. In addition, I do not know why you would go and create your own LogIn table. You should look into Microsoft Identity.
ViewModel can have multiple entities or objects from different data models or data source.
useful link:https://www.codeproject.com/Articles/826417/Advantages-of-ViewModel-in-MVC-Model-View-Controll
Example of ViewModel: As per your requirement
public class userloginviewmodel
{
public user users{get;set;}
public login logins{get;set;}
}
Controller code:
public ActionResult Create()
{
userloginviewmodel OBJuserloginviewmodel = new userloginviewmodel();
OBJuserloginviewmodel .Users = db.Users.FirstOrDefault(x => x.UserID == someId);
OBJuserloginviewmodel .logins = db.LogIns.FirstOrDefault(x => x.LoginID == someId);
return View(OBJuserloginviewmodel );
}
viewCode:
#model Coding.Models.userloginviewmodel
#{
ViewBag.Title = "Create";
}
<h2>Sign Up</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.user.name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.user.name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.user.name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Login.LoginId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Login.LoginId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Login.LoginId, "", new { #class = "text-danger" })
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Additional Note : By using the viewmodel we can achieve most of things we want, even in this, if you want list of users and logins you can just have two more properties in the same viewmodel and get the result.
Hope the above was helpful
Thanks
Karthik

Only defualt values passed to database

I'm pretty new to programming and I'm strugling with quite simple "Create" controller and view.
In the view user is declaring values, which should be passed to database. Here's the model:
public class Expense
{
public int ExpenseID { get; set; }
[DisplayName("Data")]
[Column(TypeName = "DateTime2")]
public DateTime Date { get; set; }
[DisplayName("Wartość")]
public decimal Amount { get; set; }
[DisplayName("Opis")]
public string Details { get; set; }
[DisplayName("Rodzaj")]
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string Name { get; set; }
public virtual ICollection<Expense> Expenses { get; set; }
}
In the same view I want to include partial view for managing the categories (adding, removing).
To have this working I've implemented ViewModel:
public class ExpenseCreateViewModel
{
public Expense ExpenseCreate { get; set; }
public Category CategoryCreate { get; set; }
}
And here's the code for my View:
#model Wydatki2._0.Models.ExpenseCreateViewModel
#{
ViewBag.Title = "Create";
}
<h2>Dodaj wydatek</h2>
<table class="table">
<tr>
<th>
<div>
#using (Html.BeginForm("CreateExpense", "ExpenseCreate", FormMethod.Post, new { }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.ExpenseCreate.CategoryID, "Rodzaj", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("CategoryID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.ExpenseCreate.CategoryID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ExpenseCreate.Amount, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ExpenseCreate.Amount, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ExpenseCreate.Amount, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ExpenseCreate.Date, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ExpenseCreate.Date, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ExpenseCreate.Date, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ExpenseCreate.Details, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ExpenseCreate.Details, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ExpenseCreate.Details, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Dodaj" class="btn btn-default" />
</div>
</div>
</div>
}
</div>
</th>
<th>
<div>
<input type="button" id="btn" class="btn btn-default" value="Dodaj/Usuń Kategorię" />
<p class="error">#ViewBag.Warning</p>
</div>
<div id="Create" style="display:none">
#Html.Partial("CreateCategory", Model.CategoryCreate)
</div>
</th>
</tr>
</table>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
#Scripts.Render("~/bundles/toggle")
}
The problem is that when I submit the form only the CategoryID value is passed correctly. The rest of parameters have just default values. The partial view is working well - I'm able to add or remove categories, which then are passed to the main view.
The code for my Controller:
namespace Wydatki2._0.Controllers
{
public class ExpenseCreateController : Controller
{
private WydatkiContext db = new WydatkiContext();
public ActionResult Create(bool? warn = false)
{
ExpenseCreateViewModel model = new ExpenseCreateViewModel()
{
ExpenseCreate = new Expense(),
CategoryCreate = new Category()
};
var query = from b in db.Categories
where b.CategoryID != 1
select b;
if (warn.GetValueOrDefault())
{
ViewBag.Warning = "Nie możesz usunąć tej kategorii.";
}
ViewBag.CategoryID = new SelectList(query, "CategoryID", "Name");
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateExpense([Bind(Include = "ExpenseID,Date,Amount,Details,CategoryID")] Expense expense)
{
if (ModelState.IsValid)
{
db.Expenses.Add(expense);
db.SaveChanges();
return RedirectToAction("Create");
}
var query = from b in db.Categories
where b.CategoryID != 1
select b;
ViewBag.CategoryID = new SelectList(query, "CategoryID", "Name", expense.CategoryID);
return View(expense);
}
public ActionResult CreateCategory()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateCategory([Bind(Include = "CategoryID,Name")] Category category)
{
if (ModelState.IsValid)
{
Category cat = db.Categories.FirstOrDefault(c => c.Name== category.Name);
if (cat != null)
{
if (cat.CategoryID == 1 || cat.CategoryID ==2)
{
return RedirectToAction("Create", new { warn = true });
}
else
{
db.Categories.Remove(cat);
db.SaveChanges();
return RedirectToAction("Create");
}
}
else
{
db.Categories.Add(category);
db.SaveChanges();
return RedirectToAction("Create");
}
}
return View(category);
}
I'm inclined to believe that the problem is caused by the model, which I'm passing to the view, but I really don't know, how to pass it correctly... Anyone could help with this?
You should use
([Bind(Prefix="prefixhere")]Category category)
I assume your prefix should be CategoryCreate for category, which the extension from your model ExpenseCreateViewModel, same for your CreateExpense, your prefix should be ExpenseCreate, this way you tell your controller to expect your input names after this prefix, so it will look for CategoryID and Name in create category action after the prefix you passed.
Something Like this
([Bind(Prefix="CategoryCreate")]Category category)
([Bind(Prefix="ExpenseCreate")]Expense expense)

MVC 5 - values entered on form (View) not coming through to the Controller

I created a View for form submission and I expect the form data to be passed to the Controller once the Submit button clicked.
I scaled down the Models and views a little bit for simplicity.
When I Break on the HomeController and execute the step:
ReviewViewModel reviewViewModelx = reviewViewModel;
No values are filled in from the form that I would expect.
View:
#model ReviewViewModel
<div class="panel-body">
#using (Html.BeginForm("SubmitReview", "Home", FormMethod.Post, new { model = Model }))
{
#*#Html.AntiForgeryToken()*#
#Html.ValidationSummary("", new { #class = "text-danger" })
<div class="form-horizontal">
<div class="form-group">
#Html.LabelForRequired(model => model.ReviewModel.Name, htmlAttributes: new { #class = "control-label col-md-5" })
<div class="col-md-5">
#Html.EditorFor(model => model.ReviewModel.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ReviewModel.Name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelForRequired(model => model.ReviewModel.Degree, htmlAttributes: new { #class = "control-label col-md-5" })
<div class="col-md-5">
#Html.EditorFor(model => model.ReviewModel.Degree, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ReviewModel.Degree, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-5">
<input type="submit" value="Submit Review" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
ReviewViewModel This view model contains 3 different data models, I am only showing the Review Class below as that is what is being populated.
public class ReviewViewModel
{
public VideoTypeModel VideoTypeModel;
public VideoModel VideoModel;
public ReviewModel ReviewModel;
public ReviewViewModel()
{
VideoTypeModel = new VideoTypeModel();
VideoModel = new VideoModel();
ReviewModel = new ReviewModel();
}
public ReviewViewModel(VideoTypeModel videoTypeModel, VideoModel videoModel, ReviewModel reviewModel)
{
this.VideoTypeModel = videoTypeModel;
this.VideoModel = videoModel;
if (reviewModel != null)
{
this.ReviewModel = reviewModel;
}
}
}
Review Data Model:
ReviewModel
public class ReviewModel
{
[Required]
[Display(Name="Name: ")]
public string Name { get; set; }
[Required]
[Display(Name="Degree:")]
public string Degree { get; set; }
}
HomeController
As mentioned earlier, after first step, there is no values in the reviewViewModelx.ReviewModel.Name or
reviewViewModelx.ReviewModel.Email (or any other fields that are in the model as I removed in this sample for brevity.
[HttpPost]
public ActionResult SubmitReview(ReviewViewModel reviewViewModel)
{
ReviewViewModel reviewViewModelx = reviewViewModel;
SendReviewEmail ce = new SendReviewEmail(null);
SendReviewEmailToTech ste = new SendReviewEmailToTech(null);
return View(reviewViewModel);
}
I'm somewhat of a newbie to MVC, any assistance is appreciated. Thanks in advance!
Edit: It appears the issue may be in your ReviewViewModel. I don't have an MVC project in front me, but I think you may be missing your getters and setters for the objects. Can you try something like:
public class ReviewViewModel
{
public VideoTypeModel VideoTypeModel {get; set; }
public VideoModel VideoModel {get; set; }
public ReviewModel ReviewModel {get; set; }
}
In your controller you need to add the Post tag. Typically you will have one action for the HttpGet and on for the HttpPost
public ActionResult SubmitReview()
{
return View();
}
[HttpPost]
public ActionResult SubmitReview(ReviewViewModel reviewViewModel)
{
ReviewViewModel reviewViewModelx = reviewViewModel;
SendReviewEmail ce = new SendReviewEmail(null);
SendReviewEmailToTech ste = new SendReviewEmailToTech(null);
return View(reviewViewModel);
}

How to Create Action method to Apply Job in Mvc 4?

I am creating a table called ApplyJob in my database. But my question is how to create a method which can populate the information for each job/user in the database. Any help is much appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace TopJobs.Models
{
public class ApplyJob
{
[Key]
public int a_Id { get; set; }
public int? UserId { get; set; }
public long po_ID { get; set; }
public virtual UserProfile UserPro { get; set; }
public virtual PostJob Postjobs { get; set; }
}
}
What you eould need to do is create a view where you can enter the information that you would like to save in the database via Linq. the view would look something like this:
#model TopJobs.Models.ApplyJob
#{
ViewBag.Title = "Create Job";
}
<div class="container">
<h2>Create Job</h2>
#using (Html.BeginForm("NameOfCreateActionHere", "NameOfControllerHere", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.UserId , htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserId , new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.UserId , "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.po_ID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.po_ID, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.po_ID, "", new { #class = "text-danger" })
</div>
</div>
//Continue doing this for the rest of the elements in your model
}
</div>
You will then need to create an action in your controller which will read in this information from your model and save it to your database. this action could look something like this:
public ActionResult Create([Bind(Include = "a_Id,UserId,po_ID,UserPro,Postjobs")] ApplyJob applyJob)
{
if (ModelState.IsValid)
{
db.TableNameHere.Add(applyJob);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(applyJob);
}
Where db is the instance of your database. This is just one possible way of doing it. Hope this helps.

Resources