2017-04-22 20 views
0

所以我在MVC很新,我的使命模式:如何从文本中插入数据MVC模型`

public class Mission 
{ 
    public ObjectId _id { get; set; } 
    public string MissionType { get; set; } 
    public string ElipseNumber { get; set; } 
    public string MissionDate { get; set; } 
    public string ReminderNumber { get; set; } 
    public string Notes { get; set; } 
} 

当用户选择一个特定的任务,它进入查看@ViewBag.SelectedMission

现在,我希望让用户有一个注释添加到选定的使命,选项,以便用一个模式,我添加像这样一个文本框:

<div class="notesLabel"> 
    @Html.LabelFor(model => model.Notes) 
</div> 
<div class="notesTextBox"> 
    @Html.TextBoxFor(model => model.Notes) 
</div> 

不知道到底该怎么做,怎么样o我从文本框中获取输入并将其添加到SelectedMission.Notes中?

预先感谢。

+0

你可以提交表单回到行动并获得值 – Usman

+0

@Usman:我在下面有一个代码片段。你能否看到这是否相关, – Unbreakable

+0

@Unbreakable我编辑了你的答案,使其更清楚的操作 – Usman

回答

0

我也是一个初学者,但试图帮助。据我了解,你可以这样做下面

位指示

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(Mission model) 
{ 
    var notes= model.Notes; 
    //Set the values to view model accordingly and save to DB eventually 
} 

查看

@model Mission 
    @using (Html.BeginForm("Create", "ControllerName")) 
    { 
     @Html.AntiForgeryToken() 
     // ALL YOUR HTML FIELD WILL COME HERE 
     <div class="notesLabel"> 
      @Html.LabelFor(model => model.Notes) 
     </div> 
     <div class="notesTextBox"> 
      @Html.TextBoxFor(model => model.Notes) 
     </div> 
     <input type="submit" value="save"> 
    } 
0

试试下面的代码。表单将提交用户在按钮单击时输入的详细信息。假设:控制器名称为Home,保存注释数据的操作名称为SaveNotes。当用户单击提交按钮时,数据将发送到HomeController中的SaveNotes操作。在操作功能中,验证完成后,值将被保存到数据库中。如果你不想保存到数据库,你可以根据你的逻辑/设计做任何事情。价值将在objMission.Notes

<% Html.BeginForm("SaveNotes", "Home", FormMethod.Post); %> 
    @Html.AntiForgeryToken() 
    : 
    : 
    <div class="notesLabel"> 
     @Html.LabelFor(model => model.Notes) 
    </div> 
    <div class="notesTextBox"> 
     @Html.TextBoxFor(model => model.Notes) 
    </div> 
    : 
    : 

    <input type="submit" name="submit" value="Save" /> 
<% Html.EndForm(); %> 


public class HomeController : Controller 
{ 
    : 
    : 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult SaveNotes(Mission objMission) 
    { 
     //Set the values to view model accordingly and save to DB eventually 
     if (ModelState.IsValid) 
     { 
      db.Missions.Add(objMission); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(objMission); 
    } 

    : 
    : 
} 
相关问题