我正在我的数据库中创建一个名为ApplyJob的表。但我的问题是如何创建一个方法,可以填充数据库中每个作业/用户的信息。非常感谢任何帮助。
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; }
}
}
答案 0 :(得分:0)
您需要做的是创建一个视图,您可以通过Linq输入要保存在数据库中的信息。视图看起来像这样:
@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>
然后,您需要在控制器中创建一个操作,该操作将从模型中读取此信息并将其保存到数据库中。这个动作看起来像这样:
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);
}
其中db是数据库的实例。这只是一种可行的方法。希望这会有所帮助。