How to call action method using query string in mvc - asp.net-mvc

Question: when new employee created I have to send mail to the admin. right now mail sending successfully but when admin clicks the link directly it should open the created employee partial view along with partial 1 and partial view 2 for your reference please find below the image.
i dont know how to do this one. right now directly i called the action method
Example:
it should call the following action method
//websitename/seachcountry
//websitename/listofemployee
//websitename/emplyesearchbyid
which is the best way to call from the email link.
Thanks.

To answer your question, to call an action using query string, your action should look like the following in your controller:
public ActionResult EmployeeInfo(string query)
{
// use query here
}
However, I suggest create an employee details action and view that would correspond to route like //websitename/employee/details/id.
This view must be tied to a viewmodel that contains all required info about the employee (including the data in partial views).

Related

Html.BeginForm does not redirect to action Method

I try using Html.BeginForm to submit my data but it does not help me with it. I followed this link to make my mvc program: https://www.c-sharpcorner.com/UploadFile/f82e9a/form-data-submiting-and-displaying-data-another-view-using-m/
#using (Html.BeginForm("SubmitEmp","ClientController",FormMethod.Post))
using this I get the following error:
My view has a folder named Client, in which there are two files:
Index: this contains the form.
this the view to get the details entered in the previous form.
My controller (ClientController) has the code mentioned in the link
and my Model is Cmodel with the same code.
Your Code should be like this then it will work
#using (Html.BeginForm("SubmitEmp","Client",FormMethod.Post))
We cannot and use full controller name like ClientController for specifying controller name we need to specify only the name of Client
In your URL localhost:5000/ClientController/Submitemp showing controller name ClientController it should be Client only change your URL like localhost:5000/Client/Submitemp then it will work.
Cheers!!
No need to pass "Controller" in controller name, simply pass "Client". Make sure you have "SubmitEmp" HttpPost action in Client Controller
I can see that you have passed controller name as 'ClientController' which appears to be incorrect. The Controller in 'ClientController' represents that this is a Controller named Client. You can pass 'Client' instead of 'ClientController' in the place for controller.

ASP.NET MVC controller action restricted to a specific view

Is there a way to restrict a controller action to be accessible only from a specific view? I have a Details page for a queried entity in my database and basically by a button and a simple JS confirmation prompt I would like to change few properties of this object and save it back to the database. I've developed a controller action method which does the job but I have no idea how to restrict access to it, so users cannot (un)intentionally modify entities by passing a specific url in the browser. I would like the action to be only accessible on this specific Details page, by pressing the designated button.
I tried using [ChildActionOnly] but it's only accessible from another action method, and not a view.
Thank you for your help.
Thanks to Stephen Muecke's comment I've managed to get it working.
The button is a simple Action Link, which redirects to the restricted controller action:
public ActionResult ReturnBook(int id)
{
if (Request.UrlReferrer == null)
{
return HttpNotFound();
}
//code
}
The method checks, if there is any UrlReferrer at all. Typing action url in the browser results in the referrer being null.
If the call is made by ActionLink from a View, the UrlReferrer is not null and optionally its property Request.UrlReferrer.AbsolutePath can be checked and compared to a desired url, from which the invoke can only be made. In my case, as I am not invoking this method anywhere else in my code, I stayed with just null/notnull condition.

How to call Controller action method from umbracoo

I am new to Umbracoo . I was trying to call Controller action method from umbracoo . I find the solution for how to render view but did not find any way to call action method of controller ..
for this purpose first you would have to inherit your controller from Umbraco.Web.Mvc.SurfaceController or Umbraco.Web.Mvc.RenderMvcController
have a look at:
http://our.umbraco.org/documentation/reference/mvc/surface-controllers
http://our.umbraco.org/documentation/reference/mvc/custom-controllers
Make sure that your view/partialView is created from umbraco backend as template so that it would have it's record save in database through unique id.
it would be hidden in your solution explorer first show all hidden items and the include in your project and start working on it.

How to organize controller class and action method?

Suppose I am working on a Car Portal. To search a new car, the user provides the following information:
Brand
Model
ModelVersion
FuelType
Budget
Business Rules:
If user selects Brand only : Show brand list page with all models of selected brand
If user selects Brand and Model : Show Model List page
If user selects Brand + Model + Model version : Show model version details page.
if user selects Fuel type OR Budget : Show Brand list page.
Now I have two approaches to define controller and action.
Approach 1: One Controller class with multiple action method
Controller Class : CarSearchmanager
with following are Action Methods:
- SearchNewCar(int Barnd,int Model.......)
Depending on user selection this method will redirect control to following
action method:
- BrandListing(int BrandID......)
- ModelListing(int BrandID,intModelID.....)
- ModelVersionDetails((int BrandID,intModelID,int ModelVersionID....)
Approach 2: Multiple controller class
Controller Class : CarSearchmanager
Following are Action Methods:
- SearchNewCar(int Barnd,int Model.......)
Depending on user selection this method will redirect control to following
controller action method:
Then I will have separate controller class and action method for each of the pages like
bellow:
- BrandListing
- ModelListing
- ModelVersionDetails
I am very confused about how to organize the controller class and action methods. Is there any best practice kind of document? Please suggest one to me.
I do not think there is a defined best version. Define it in such a way that you feel more clean and organized. From my understanding of your requirement, i may define like this
BrandController
action methods
ListForFuleAndBudget(string fuelType,string budget)
List(string brandName)
ModelController
action method
List(string brand,string model)
Details(string brand, string model, string version)
Now if you want nice url where you do not want the action method names as ot is, (ex :details) you can define your pretty urls when registering your routes in global.asax, before the generic route definition.
routes.MapRoute("list", "model/{brand}/{model}",
new { controller = "brand", action = "List");
routes.MapRoute("list", "model/{brand}/{model}/{version}",
new { controller = "brand", action = "details");
//default route definition goes here
Now yoursitename/model/honda/camry will take the user to the list action method and
yoursitename/model/honda/camry/lx will take them to the details action method.
my suggestion is to have this in the same controller
Since this is a search functionality which will be used
In a similar context. Use method overloading and in the
Corresponding methods return the particular view.

How to design a complex form that requires search results from a different MVC controller

I want to create a form for creating an order. So I have an 'Order' controller. One of the first steps will be to select an existing person to send this order to.
I'm thinking the Create() action method on OrderController will put a new Order object in session. Then I'll need a link that will redirect to another controller, then return a customerID int to this Create() method.
I will have either a SearchController with a FindCustomer() action method, or a Search() action method on the CustomerController.
I have tried the first way. But what I am doing looks pretty messy. Especially considering that I'll need to do this at least one more time on this form, redirecting to the ItemsController to return items to add to the order.
What's the best way to design communication like this between controllers?
I'm not sure why you think you need other controllers for this. In your GET Create action, put the available Customers and Items in to ViewData, and then in your view put some controls for the user to select values.
Then they will be POSTed to your POST Create action, and you can bind & save it in your Order object. You could have a separate action for adding Items to the Order if that gets too complex. But it could still be on the same OrdersController.

Resources