MVC4形式的保存到实体框架模型 [英] MVC4 forms that save to entity framework models

查看:107
本文介绍了MVC4形式的保存到实体框架模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用EF code以下课程模型创建:

I have the following course model created using EF code first:

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字段:

In design mode I have the following HTML fields:

<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>

我怎么能保存在文本框中输入时提交按钮实体框架数据库中的数据是pressed?

How can I save the data entered in the text boxes to the entity framework database when the submit button is pressed?

推荐答案

当您在模型视图发送,使用类似

When you send in the model to the view, use something like

@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"/>
}

到模型视图,从而将使其回来后绑定当您单击submitt按钮。

to bind the model to the view, which in turn will make it post back when you click the submitt 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);
}

这篇关于MVC4形式的保存到实体框架模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆