Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

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


Can some one help me please?
Thank you in advance

Luai
Posted

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"))


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++)


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
 
Share this answer
 
Hi,

You need to create a Main View and need to render 3 different views(may be partial) within it.
consider
XML
MvcApplication2.Models.CSkillsViewModel
as your model
XML
<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

C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900