用剃须刀在一个视图中使用两种模型

问题描述

| 我有一个需要使用2个模型的视图,因为我正在尝试自动填充框。视图是要添加一个类似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。 这是我用剃刀的第一天,很抱歉那些愚蠢的问题:/ 我在stackoverflow上看到了有关此问题的其他问题,但我认为答案对我没有帮助。 我可以在视图上更改什么来运行此页面?     

解决方法

        您应该创建一个具有QuestionModel和TagModel属性的视图模型(例如,QuestionTagViewModel)。然后将QuestionTagViewModel传递到要在其中使用的视图。 更新: 例
public class QuestionTagViewModel {
    public QuestionModel {get;set;}
    public TagModel {get;set;}
}
您将可以像这样使用它:
Model.QuestionModel.Content
和:
Model.TagModel.Name
看一个例子:Viewmodels     ,        或者您可以像这样通过控制器将元组传递给视图
return View(new Tuple<QuestionModel,TagModel>(){...});