0

我首先使用 EF 代码创建了以下课程模型:

public class Course
{
    [Key]
    public int CourseID { get; set; }

    [StringLength(50)]
    public string Name { get; set; }

    [StringLength(200)]
    public string Description { get; set; }

    [StringLength(50)]
    public string Author { get; set; }

    public DateTime UploadDate { get; set; }

    public DateTime ExpiryDate { get; set; }

    public int ParticipationPoints { get; set; }

    public string BlobURL { get; set; }

    //1 to many relationship
    public virtual ICollection<Audit> Audits { get; set; }

    public virtual ICollection<Content> Contents { get; set; }

    public virtual ICollection<Enrollment> Enrollments { get; set; }

    public virtual ICollection<Feedback> Feedbacks { get; set; }

    public virtual ICollection<Keyword> Keywords  { get; set; }

    public virtual ICollection<Target> Targets { get; set; }

    public virtual ICollection<CourseSection> CourseSections { get; set; }

    //many to many relationship
    public virtual ICollection<MetaLearningUser> MetaLearningUsers { get; set; }

}

在设计模式下,我有以下 HTML 字段:

<form class="custom">
       <fieldset>
         <div class="row">
           <div class="twelve columns">
             <div class="row">
               <div class="six columns">
                 <label>Course Title</label>
                 <input type="text">
               </div>
               <div class="six columns">
                 <label>Author</label>
                 <input type="text">
               </div>
             </div>

             <div class="row">
               <div class="six columns">
                 <label>Keywords</label>
                 <input type="text">
                </div>
                <div class="six columns">
                  <label>Metadata</label>
                  <input type="text">
                </div>
             </div>

             <div class="row">
               <div class="twelve columns">
                 <label>Description</label>
                 <textarea></textarea>
               </div>
             </div>

             <div class="row">
               <div class="six columns bottom20">
                 <label>Media type</label>
                   <div class="custom dropdown"> <a href="#" class="current"> Select media type </a> <a href="#" class="selector"></a>
                     <ul>
                       <li>Screencast</li>
                       <li>Podcast</li>
                       <li>Document</li>
                       <li>SCORM</li>
                     </ul>
                   </div>
               </div>
               <div class="six columns bottom20">
                 <label>Directory</label>
                   <div class="custom dropdown"> <a href="#" class="current"> Select subject </a> <a href="#" class="selector"></a>
                     <ul>                       
                       <li>Human Resources</li>
                       <li>IT Security</li>
                       <li>Corporate Governance</li>
                       <li>Health &amp; Safety</li>
                       <li>Legal</li>
                       <li>IT Infrastructure</li>
                       <li>3rd Parties</li>
                     </ul>
                   </div>
               </div>
           </div>
           <div class="row">
             <div class="six columns">
               <label>Start date</label>
               <input type="date" class="datefield">
             </div>
             <div class="six columns">
               <label>End date</label>
               <input type="date" class="datefield">
             </div>
           </div>
           </div>
         </div>
         <a href="#" class="medium button bottom20">Submit</a>
        </fieldset>
      </form>
    </div>

按下提交按钮时,如何将文本框中输入的数据保存到实体框架数据库中?

4

1 回答 1

1

当您将模型发送到视图时,请使用类似

@using(Html.BeginForm("Index"))
{
   @Html.TexboxFor(x => x.Name) // istead of <input type=text>
   @Html.TextborFor(x => x.Description)
   <input type="submit" value="Submit button"/>
}

将模型绑定到视图,这反过来会在您单击提交按钮时使其回发。

在控制器中,您有类似的东西

[HttpPost]
public ActionResult Index(CourseViewModel model) // because you'd want to use a 
// view model and not the entity itself
{
// check if the model is valid
// maybe some more logic
db.SaveChanges();
return View(model);
}
于 2013-07-01T16:59:03.217 回答