7

Problem is: I want to run 3 different actions but instead of that i want to fed all data from single action in a bigger model.

I am using:

public class SearchScrapClass
    {
        public WClass WClass { get; set; }
        public SClass SClass { get; set; }
        public YClass YClass { get; set; }
    }

    public class WClass 
    {
        public string title { get; set; }
        public string link { get; set; }
    }
    public class SClass 
    {
        public string title { get; set; }
        public string link { get; set; }
    }
    public class YClass 
    {
        public string title { get; set; }
        public string link { get; set; }
    }

I am using LINQ to add data in these models.

I am using :

      var wikians = from info in document.DocumentNode.SelectNodes("//div[@id='span']")
        from link in info.SelectNodes("div//a").Where(x => x.Attributes.Contains("href"))
         select new SearchScrapClass //Main Bigger Class
         {
            WClass.link= link.Attributes["href"].Value, //ERROR: How to add to WClass's url ?
            WClass.title= link.InnerText //ERROR: How to add to WClass's url ?
         }


var wikians = from info in document.DocumentNode.SelectNodes("//div[@id='results']")
               from link in info.SelectNodes("p//a").Where(x => x.Attributes.Contains("href"))
               select new SearchScrapClass //Main Bigger Class
                 {
                   YClass.link= link.Attributes["href"].Value, //ERROR: How to add to YClass's url ?
                   YClass.title= link.InnerText //ERROR: How to add to YClass's url ?
                 }

//Also for the 3rd class (model)


    return View(wikians); //and then return bigger class model so that i can access them in view

This is one way i want to add data to link and title of all the classes.

My try is to add data to all 3 classes from different sources and pass the bigger model to view so that i can access all the classes as:

@model SearchScrapClass
@using(Html.BeginForm()) {
    @Html.EditorFor(o => o.WClass.link)
    ...
}

Please suggest a way

Thanks

4
  • 4
    create a viewmodel that ties all three models together and type the view to that viewmodel. Commented Jun 5, 2012 at 16:44
  • I'm confused as to why you have 3 different classes that all look the same? Why not just use 3 different instances of the same class? Commented Jun 5, 2012 at 16:52
  • @hermiod : Actaully the logic i have shown looks similar [little modified] but the sources from where they fetch url and content is different. So i need different classes. Commented Jun 5, 2012 at 16:58
  • @Brian is correct. The "correct" way to do this in MVC is to create a ViewModel containing these three classes. The classes are data models. The ViewModel is something else, it should contain everything your View needs to render correctly. Commented Jun 5, 2012 at 17:14

4 Answers 4

8

To Expand on my comment, I would suggest creating a ViewModel folder for organization sake. in this add the view model

public class SearchScrapClassViewModel
{
    SearchScrapClass searchScrap;
    WClass wClass;
    SClass sClass;
    YClass yClass;
}

In your controller then you instantiate the new viewmodel

SearchScrapClassViewModel model = new SearchScrapClassViewModel
{
    ....add in your logic to fill your class objects here


}

return view(model);

then in your view add the using for the viewmodel.

@using SearchScrapClassViewModel
Sign up to request clarification or add additional context in comments.

1 Comment

Could you elaborate on the views side. How would I access each element w, s, y individually in the view?
1

You can pass multiple model by creating a new model class which will contain multiple objects.

public class MultiModel 
    {
     SearchScrapClass searchScrap;
     WClass wClass;
     SClass sClass;
     YClass yClass;
    }

2 Comments

And then how do you receive / use that class in the view?
Brian has explain this further you can use Modal in view to access each class e.g. @Model.SearchScrapClass, Modal.WClasss
1

See the tuple tutorial http://www.dotnetperls.com/tuple or this one http://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx

Like controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var first = new FirstModel();
        var second = new SecondModel();
        return View(Tuple.Create(first,second));
    }
}

And the view:

@model Tuple

<div> 
    @Model.Item1.FirstModelProp
    @Model.Item2.SecondModelProp
</div>

Comments

0
SearchScrapClassViewModel model = new SearchScrapClassViewModel
{
    ....add in your logic to fill your class objects here


}

what logic we apply here " ....add in your logic to fill your class objects here"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.