Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8072663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:05:26+00:00 2026-06-05T14:05:26+00:00

After writing webforms for many years, I am trying to make the transition to

  • 0

After writing webforms for many years, I am trying to make the transition to MVC. I’m having trouble getting away from Datatables and understanding how to create a model class properly. The big hiccup is my datasource is a restful webservice which returns XML.

Currently I am strongly typing the view’s model as a datatable. Am I giving anything up by not using a model class? Am I including to much information in the controller?

The following is just a simple application which takes a querystring value, performs a search using a restful webservice, and spits out the results on the page. It works, but I don’t feel like I’ve created an actual MVC application.

My Controller:

using System.Web.Mvc;
using System.Data;
using System.Configuration;

namespace mvc1.Controllers
{
    public class HomeController : Controller
    {
        dhRestful _datahandler = new dhRestful(ConfigurationManager.ConnectionStrings["service"].ConnectionString);

        //
        // GET: /Home/
        public ActionResult Index(string search = "")
        {
            ViewBag.Title = "You searched for: " + search;

            using (DataTable dt = _datahandler.get("tv_show?name=" + search))
            {
                return View(dt);
            }
        }

    }
}

dhRestful is a helper class I ported from a previous project. It makes restful webservice calls and serializes the response XML into a DataTable. If I should continue using this class, where should I be putting it in the project files?

using System.Data;
using System.Xml;

namespace mvc1
{
    public class dhRestful
    {
        string _hostPath = "";

        public dhRestful(string hostPath)
        {
            _hostPath = hostPath;
        }

        public DataTable get(string partialUrl)
        {
            using (XmlTextReader xr = new XmlTextReader(_hostPath + partialUrl))
            {
                using (DataSet ds = new DataSet())
                {
                    ds.ReadXml(xr);

                    if (ds.Tables.Count > 0)
                    {
                        return ds.Tables[0];
                    }
                    else
                    {
                        return new DataTable();
                    }
                }
            }
        }
    }
}

The View using razor:

@model System.Data.DataTable

<h2>@ViewBag.Title</h2>

<table border="1">
    <thead>
        <tr>
            <th>ID</th>
            <th>Show Name</th>
        </tr>
    </thead>
    <tbody>
        @foreach (System.Data.DataRow row in Model.Rows)
        {
            <tr>
                <td>@row["id"].ToString()</td>
                <td>@row["name"].ToString()</td>
            </tr>
        }       
    </tbody>
</table>

Sample XML where the search was on the name “Stargate”:

<myService>
  <tv_show>
    <id>72449</id>
    <name>Stargate SG-1 </name>
  </tv_show>
  <tv_show>
    <id>83237</id>
    <name>Stargate Universe </name>
  </tv_show>
  <tv_show>
    <id>70852</id>
    <name>Stargate: Infinity </name>
  </tv_show>
  <tv_show>
    <id>70851</id>
    <name>Stargate Atlantis </name>
  </tv_show>
</myService>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:05:27+00:00Added an answer on June 5, 2026 at 2:05 pm

    I would recommend you to write this data access layer via dependency injection, at Composition root. So later on instead of a rest client you can inject a different client that satisfies a contract.

    Another note, is to create view models. If you are VO’s or DTO’s that you are using directly within you application it is a good practice to wrap them. I guess this is your answer. You can have your view Models in Model folder or you can create subfolder etc.

    Something like this:

       // you need to adjust your dependency accordingly
       Interface IRepository<T> : where t has a value
       {
         void Set(T item);
         T Get(some value that you indicate in T);
       }
    
        namespace mvc1.Controllers
        {
            public class HomeController : Controller
            {
                IRepository<SomeObject> _repo;
    
                public HomeController(IRepository<SomeObject> repository)
                {
                   _repo = repository;
                }             
    
    
                //
                // GET: /Home/
                public ActionResult Index(string search = "")
                {
                    ViewBag.Title = "You searched for: " + search;
    
                    using (SomeObject obj = _repo.get("tv_show?name=" + search))
                    {
                        // here you can use automapper to map a DTO or VO to View model.
                        FooViewModel model = someobject;
                        return View(model);
                    }
                }
    
            }
        }
    

    At composition root: global.asax you can bootstrap the container. Many ways to this, look at structure map and how to use container.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After writing and reading an xml string to and from a stream, it ceases
After writing deployment scripts from within the ISE, we need our continuous integration (CI)
So after writing a large .tex file and using many packages I want to
i want to make my server side in php. after writing the code in
After writing code to populate textboxes from an object, such as: txtFirstName.Text = customer.FirstName;
I'm trying to delete a file, after writing something in it, with FileOutputStream .
After writing so many for loop, I could not find to name iterator. I
I'm having an EditText and a Button in my Frame using C#. After writing
I am trying to write a python wrapper for a C function. After writing
I am having error closing streamwriter after writing json content to the stream writer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.