Web API return Helper Page - asp.net-web-api2

I have a ASP.NET WebPage and like to use the Helper Pages located under App_Code. Now I don't have any clue how to solve this problem: I post data from a html form to my Controller (WebApi 2 HttpPost method) with $.ajax. As a result, I wan't the content of my helper page, whose binding data comes from my controller web api method.
I would like to maintain my form data encapsulated in my model class, because it seems cleaner to me to handle with the data. I also want to access my model in my view.
[HttpPost]
[Route("api/{ClassID}/AddUsers")]
public IHttpActionResult AddUsers([FromUri] int ClassID, [FromBody] Models.UserInfo userInfo)
{
Foo result = new Foo();
result = doSometStuff(userInfo);
return ???(HelperPages.FooHelper.Get(result));
}

I have to answer my question myself. Web API only returns data, not any view. So I just have to use a mvc controller to do so.

Related

Umbraco with MVC Controller

I am working on MVC and i started learning Umbraco, I didn't get how to bind the umbraco page with mvc controller get method to show the database values. can anyone suggest any url or video?
Thansk...
What you're looking for is Umbraco route hijacking.
You can read about it here.
https://our.umbraco.org/documentation/reference/routing/custom-controllers
It's easiest to demonstrate with an example : let's say you have a Document Type called 'Home'. You can create a custom locally declared controller in your MVC web project called 'HomeController' and ensure that it inherits from Umbraco.Web.Mvc.RenderMvcController and now all pages that are of document type 'Home' will be routed through your custom controller! Pretty easy right :-) OK so let's see how we can extend this concept. In order for you to run some code in your controller you'll need to override the Index Action.
So, basically, you "simply" need to create a controller named after your document type, so for example, a document type with the name "TextPage" would need a controller called "TextPageController". Now, if you read through the documentation, you'll find that your "TextPageController" will need to inherit from the RenderMvcController. Here's an example how to achieve this.
public class TextPageController : RenderMvcController
{
public ActionResult Index()
{
return View("~/Views/TextPage.cshtml");
}
}
This forum link may help you:
https://our.umbraco.org/forum/developers/razor/38242-Umbraco-MVC4111-Surface-controller-using-an-AJAX-form

How can I use asp.net mvc routing to call a different ActionResult method and display a different URL than what is actually there?

Lets say I have a Controller class called ProductController.
I'll use Samsung as a hypothetical product category.
I have an Html.ActionLink on a page that looks like this
~/Product/Samsung?t=Sony
Now lets say I don't have an ActionResult method named Samsung.
But lets say I have more links like the one above like
~/Product/Toshiba?t=LG
~/Product/Sony?t=Dynex
Never mind the t=? part.
Is there a way that I can make all of these ActionLinks hit the same ActionResult method in my Product Controller?
public ActionResult TelevisionCategory()
{
}
Like some sort of URL routing so that any URL in a specific format will hit the TelevisionCategory method?
Sounds like you just need a custom route that takes an identifier as a second URL segment instead of an action name.
Something like the following should suffice (you'll need to add this above the default route in your route config):
routes.MapRoute(
"Products",
"Product/{category}",
new { controller = "Product", action = "TelevisionCategory" }
);
Then, your action method should look like this:
public ActionResult TelevisionCategory(string category)
See here for more on creating custom routes.
If using MVC5 is an option, you can also use attribute routing, which is a little more intuitive and succinct.

Can Umbraco pass a model to view

I am quite new to this Umbraco MVC.
I need to pass some data bound to a model to my partial view from the GET action method.
This simply is not working in a regular MVC way.
[httpget]
public ActionResult Membership()
{
SupplierMembershipInfoModel mm = new SupplierMembershipInfoModel();
mm.ProductPackage = "sssssssss";
ViewBag.status = Request.QueryString["status"];
return PartialView("MembershipPartial", mm);
}
my view:
#model Umbraco.Web.Models.SupplierMembershipInfoModel
some html.....
<td>#Model.ProductPackage</td>
I don't get data here...and the debug never hits the action. But it hits any POST action method.
I know i am doing something wrong...but just don't know what the mistake is??
Any ideas??
As #Sebastiaan points out, the best place to start is the Umbraco community site. There is documentation specific to your issue here: http://our.umbraco.org/documentation/Reference/Templating/Mvc/child-actions
In a nutshell, you want to display a child action on your page and Umbraco uses SurfaceControllers for this. A SurfaceController is simply a Controller that inherits from Umbraco.Web.Mvc.SurfaceController. This provides you Controller with access to the Umbraco context - see here (http://our.umbraco.org/documentation/Reference/Templating/Mvc/surface-controllers).
Either way, you should read the whole documentation section on templating as it will give you a lot of insight into how Umbraco MVC is managed.

Simple lack of understanding - MVC Routing

I want to call a method in the controller. To do this, assuming default routing, I have to have a view that matches the controller I'm calling.
So if I have a contoller action
public ActionResult Edit(booking booking)
then I must have a view called Edit.
Is that right?
What I want to do is call any action in my controller from a given page without there being a view of the same name.
So if I'm on the Edit page, I should be able to call an action named createproduct, without there being a createproduct view.
You don't need to have a view for every controller action. You can just call an action by calling the correct URL, that's one of the advantages of MVC and routing. Routes don't have to represent a physical location. You should be able to do the following /Controller/Edit/booking
A controller action doesn't have to return a View. You tend to see the action method return View(), which renders a view with the same name as the action method by default. But you can return View("SomeOtherViewName"). You can also return various other results, such as FileResult (returns a file), HttpStatusCodeResult (returns an HTTP response code with no content), JsonResult (returns Json), Content (returns some string) etc. The Controller class has methods to help return some of these result types: File, Json, Content.

How to expose and call methods on MVC 4 user controls

I am converting my asp.net site to MVC 4. My site has a control called loginbox that prompts the user for username and password. The control also exposes a method called IsLoggedIn that a hosting page can call. I want to continue to encapsulate the login logic in my loginbox control and call it in a similar fashion from a parent level page (i.e loginBoxInstance.IsLoggedIn()). How do I do that?
MVC doesn't have a concept of user controls. The whole setup of MVC is to separate logic from the view. You could achieve a similar setup by creating a separate controller and a partial view.
Then in your main view you could call RenderAction on the controller, which renders the partial view. However, this is only valid for the rendering stage, so something like IsLoggedIn() is not something you can (or should) do in MVC.
Example:
Controller
public class LoginController
{
public ActionResult Login()
{
return PartialView();
}
}
Partial View
// Place this file in Views/Login/Login.cshtml
<div>
<!-- Your markup -->
</div>
Main view
#Html.RenderAction("Login", "Login")
This will allow you to separate the view part (and also the logic) of the login rendering into a separate controller and view, which can be included in another view.
However, what you probably want is something like Action-attributes or inherit from a base controller class which handles all this for you.
The paradigm of MVC versus Web Forms is very different, and I think you should look into a more appropriate way of doing this.