微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决用剃须刀在一个视图中使用两种模型

| 我有一个需要使用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>(){...});
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?