2011-06-18 60 views
1

我有一個視圖,我需要使用2個模型,因爲我試圖autocmplete框。查看是添加一個問題,如在stackoverflow。您必須輸入內容,並從自動完成中選擇標籤。在剃刀上使用兩個模型在一個視圖

public class QuestionController : Controller 
    { 
     public ActionResult Add() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult Add(QuestionModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       return RedirectToAction("Index", "Home"); 
      } 
      else 
      { 
       return View(model); 
      } 
     } 

     public ActionResult TagName(string q) 
     { 
      var tags = new List<TagModel> 
          { 
           new TagModel {Name = "aaaa", NumberOfUse = "0"}, 
           new TagModel {Name = "mkoh", NumberOfUse = "1"}, 
           new TagModel {Name = "asdf", NumberOfUse = "2"}, 
           new TagModel {Name = "zxcv", NumberOfUse = "3"}, 
           new TagModel {Name = "qwer", NumberOfUse = "4"}, 
           new TagModel {Name = "tyui", NumberOfUse = "5"}, 
           new TagModel {Name = "asdf[", NumberOfUse = "6"}, 
           new TagModel {Name = "mnbv", NumberOfUse = "7"} 
          }; 

      var tagNames = (from p in tags where p.Name.Contains(q) select p.Name).Distinct().Take(3); 

      string content = string.Join<string>("\n", tagNames); 
      return Content(content); 
     } 

    } 

命名空間Szamam.Models { 公共類QuestionModel { 私人字符串_content;

public string Content 
    { 
     get { return _content; } 
     set { _content = value; } 
    } 

    public UserModel Creator 
    { 
     get { return _creator; } 
     set { _creator = value; } 
    } 

    public DateTime CreationDate 
    { 
     get { return _creationDate; } 
     set { _creationDate = value; } 
    } 

    public List<TagModel> TagModels 
    { 
     get { return _tagModels; } 
     set { _tagModels = value; } 
    } 

    private UserModel _creator; 

    private DateTime _creationDate; 

    private List<TagModel> _tagModels=new List<TagModel>(); 
} 

}

而且有一種觀點

@model Szamam.Models.QuestionModel 
@{ 
    ViewBag.Title = "Add"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2> 
    Add</h2> 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.autocomplete.js")" type="text/javascript"></script> 
<link href="@Url.Content("~/Scripts/jquery.autocomplete.css")" rel="stylesheet" type="text/css" /> 
@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>QuestionModel</legend> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Content) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Content) 
      @Html.ValidationMessageFor(model => model.Content) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.CreationDate) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.CreationDate) 
      @Html.ValidationMessageFor(model => model.CreationDate) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.TagModels) 
     </div> 
     @*here is a place for autocomplete for tags *@ 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.Name) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) 
    </div> 
     <script type="text/javascript"> 

      $(document).ready(function() { 
       $("#TagName").autocomplete('@Url.Action("TagName", "Question")', { minChars: 3 }); 
      }); 

     </script> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 


    </fieldset> 
} 

五言有我一個問題:

@*here is a place for autocomplete for tags *@ 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Name) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) 
     </div> 

在這一部分,我想有另一種模式 - TagModel。

這是我第一次漫長的一天剃鬚刀那些愚蠢的問題,很抱歉:/

我見過關於在計算器等問題,但我認爲,答案是不適合我很有幫助。

我可以在視圖上更改哪些內容來運行此頁面?

回答

6

你應該創建一個包含QuestionModel和TagModel屬性的視圖模型(eg.QuestionTagViewModel)。然後通過QuestionTagViewModel你想使用它的視圖

更新

public class QuestionTagViewModel { 
    public QuestionModel {get;set;} 
    public TagModel {get;set;} 
} 

你將能夠使用它像這樣:

Model.QuestionModel.Content 

和:

Model.TagModel.Name 

看看這裏的一個例子:Viewmodels

5

或者你也可以傳遞一個元組從控制器查看這樣

return View(new Tuple<QuestionModel, TagModel>(){...});