0

Very new to MVC development, and for our project, we're communicating RESTfully with a SQL database. In my case, I have two Controllers, DrugEntriesListController and DrugEntryController. The former is derived from Controller and controls a web page view, while the latter is derived from ApiController and handles RESTful HTTP POST/GET with our database through a repository class (tutorial I followed is this one).

In my web page, you can upload a file (using this tutorial), which routes to the DrugEntriesListController via the built-in Request class that tracks uploaded files. From there, I can send said files to be parsed, etc. However, my issue is this: in order to RESTfully upload the parsed data to the DB, I need to go through DrugEntryController, since it is an ApiController and has the HTTP POST/GET calls. Problem is, I have no idea how to access it without instantiating a new one from either the view Controller or other files.

Perhaps I'm misunderstanding (very likely), but the ApiController is interacted with via the DrugEntriesList view Index.cshtml, here's my current code:

@{
  ViewBag.Title = "Index";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="body">
  <ul id="drug_entries"></ul>
</div>

@section scripts{
<script type="text/javascript">
  $(function ()
  {
    $.getJSON('/api/DrugEntry', function (drugEntriesJsonPayload)
    {
      $(drugEntriesJsonPayload).each(function (i, item)
      {
        $('#drug_entries').append('<li>' > + item.NDC + '</li>');
      });
    });
  });
</script>
}

Upload a new .CSV file for the Drug Database:

@using (Html.BeginForm ("", 
  "DrugEntriesList", 
  FormMethod.Post, 
  new {enctype="multipart/form-data"}))
{
  <input type="file" name="newCSV" /><br />
  <input type="submit" name="Submit" id="Submit" value="Upload" />
}

Since I've got a wall of obfuscated gibberish, the tl;dr is that I need to get information received in a Controller into an already existing ApiController.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Befall
  • 6,560
  • 9
  • 24
  • 29
  • 1
    Just a couple of notes, it is typically best to actually put your code here and not on a remote site (Although including a link with code is ok). I removed your visual studio tag as this really doesn't integrate with that product. ([I also removed the redundant tags from your title](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles)). – Erik Philips Feb 16 '14 at 01:42
  • Can I ask you why you believe you need an ApiController for this project (is it required or your just trying to use them)? – Erik Philips Feb 16 '14 at 01:47
  • I noticed your edits and will change future posts accordingly, thank you. I'm not 100%, honestly, but I believe my groupmate made it seem like we needed it to properly HTTP Post/Get RESTfully. The difference between the controllers is still a bit blurry to me, I just know that ApiController doesn't have the built in Request class that I need to retrieve uploaded files. – Befall Feb 16 '14 at 01:49

1 Answers1

0

So, I'm going to state a religious answer (that is this is how I decide to program, not a best practice or defacto way).

I don't use the ApiController unless; it's going to be called by an external 3rd party, or I am using a 3rd party framework (Kendo, Knockout) that is designed to work directly against a RESTful service.

In your specific case, there is no reason you have to use an ApiController. Your code could look like:

[Script]

$.getJSON('/DrugEntry/GetList', //...

[c#]

public class DrugEntryController
{
  [HttpPost]
  public ActionResult GetList()
  {
    // blah blah get list
    IEnumerable<DrugEntry> drugs = db.GetDrugs();

    return this.Json(drugs);
  }
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • http://stackoverflow.com/questions/9494966/difference-between-apicontroller-and-controller-in-asp-net-mvc - This is basically saying for RESTful implementation with a web client I should 100% use ApiController, is there no way to get this stuff via ApiController? – Befall Feb 16 '14 at 23:15
  • Also, the script you show is regarding getting my information and displaying it, which is a separate issue I'm having (http://stackoverflow.com/questions/21793600/vs-2012-restful-api-cant-find-display-info-in-browser/21794388?noredirect=1#comment33000487_21794388). I was more referring to the latter code shown, where I upload a file and have it properly get to the ApiController class. – Befall Feb 16 '14 at 23:20
  • Ended up using http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2 as I couldn't figure this way out. Thanks for the help, though! – Befall Feb 17 '14 at 03:24