一个控制器中的多个模型,返回所有值 [英] Multiple model in one controller, return all values

查看:56
本文介绍了一个控制器中的多个模型,返回所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个包含用户信息的类(来自用户表的一条记录),以及用户技能列表(UserSkills),还有技能列表(来自表技能)



我需要能够允许网站用户编辑用户信息,以便更新所选用户技能(使用多CheckBoxList或CheckBox)。所有这一切都在一个视图中(如果有多个局部视图则无害)



控制器动作,Get和Post将如何显示?



Hi All,
I have a class that contains User info (one record from table User), in addition to List of user skills (UserSkills), also List of Skills (from table Skills)

I need to be able to allow the user of the site, to edit the User info, in addion to update the selected user skills (using multi CheckBoxList or CheckBox. All this in one View (no harm if multiple partial view)

How would the controller Action, for Get and Post will look like?

public class SkillsViewModel
    {
        public IList<Skill> AvailableSkills { get; set; }
        public IList<UserSkill> SelectedSkills { get; set; }
        public User Usr { get; set; }
    }





有人可以帮我吗?

提前谢谢



Luai



Can some one help me please?
Thank you in advance

Luai

推荐答案

最终......

我发现解决方案,在Edit.cshtml中,我补充说:

Finaly...
I found the solution, in the Edit.cshtml, I added:
@model MvcApplication.Models.SkillsViewModel





我也用每个块分隔:





also I separated each block with:

@using (Html.BeginForm("Edit", "UserDetail"))





取决于哪个Action以及它应该做什么,然后我注意到checkboxList以错误的方式编写(foreach ..)我已将其修复为:





depending om which Action and what it should do, then I have noticed that the checkboxList is written in a wrong way (foreach..) I have fixed it to be:

for (int i = 0; i < Model.AvailableSkills.Count; i++)





最后在每个块的Action(HttpPost)中,我检索了相应的变量,List和类:





Finally inside the Action (HttpPost) for each block, I retrieved the corresponding variables, List, and class:

public ActionResult ValidSkills(SkillsViewModel Sk )
        {
            if (ModelState.IsValid)
            {
                //Save/Update data into database
                >>>>
                return View("FeedBak",Sk);
            }
            return View();
        }





请记住,在视图中,您需要获取需要检索的数据,并将其写为:



Keep in mind that in the view, you need to have the data which u need to retrieve, written as:

@Html.HiddenFor(model => model.Usr.Id)
    @Html.HiddenFor(model => model.Usr.UserId)





谢谢



Thank you


您好,



您需要创建一个主视图,并需要在其中呈现3个不同的视图(可能是部分的)。

考虑
Hi,

You need to create a Main View and need to render 3 different views(may be partial) within it.
consider
MvcApplication2.Models.CSkillsViewModel

作为您的模型

as your model

<pre lang="xml">&lt;pre lang=&quot;xml&quot;&gt;&amp;lt;fieldset&amp;gt;
        &amp;lt;legend&amp;gt;Fields&amp;lt;/legend&amp;gt;
        &amp;lt;legend&amp;gt;Fields&amp;lt;/legend&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;% {Html.RenderAction(&amp;quot;UserPartialView&amp;quot;, Model.Usr);} %&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div &amp;gt;
            &amp;lt;% {Html.RenderAction(&amp;quot;UserPartialView&amp;quot;, Model.AvailableSkills);} %&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div &amp;gt;
           &amp;lt;% {Html.RenderAction(&amp;quot;UserPartialView&amp;quot;, Model.SelectedSkills);} %&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;p&amp;gt;
            &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Create&amp;quot; id=&amp;quot;sub&amp;quot; /&amp;gt;
        &amp;lt;/p&amp;gt;
    &amp;lt;/fieldset&amp;gt;&lt;/pre&gt;</pre>





在控制器中你需要写这样的动作





In Controller you need to write actions like this

public class MyTestController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(Models.CSkillsViewModel mod)
        {
            if (ModelState.IsValid)
            {
                //Save/Update data into database
                ViewData["result"] = "Data Successfully saved";
            }
            return View();
        }

        public PartialViewResult User(Models.User us)
        {
            return PartialView(us);
        }

        public PartialViewResult AvailableSkills(Models.Skill sk)
        {
            return PartialView(sk);
        }
        public PartialViewResult UserSkills(Models.UserSkill usk)
        {
            return PartialView(usk);
        }
    }





我认为您需要将这些操作呈现为



I think you need to render these actions as

"Html.renderPartial(...)"





这不是完整的解决方案。只是一个想法。



This is not complete solution. Just an Idea.


这篇关于一个控制器中的多个模型,返回所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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