I've got a certain action in my controller.
I've created a view for it and linked a model in it... now I want to pass some info from a property from this model onto another action safely... Only ways I know are to pass it with #Html.ActionLink /with a hidden field in a form.
But these aren't secure at all as far as I know... so what other way is there to do this ?
You can store it in a session variable:
To store a property of the model in the first action:
public ViewActionResult SomeAction(SomeModel model)
{
Session["remember"] = model.someProperty;
return View();
}
To retrieve it in another action:
public ViewResult SomeOtherAction()
{
var rememberedValue = Session["remember"];
return View();
}
Related
I'm trying to pass the id value of the url into a textboxfor of a view like the picture below,and i have no idea how to do it.
here's the image
update, the problem is that value of id is pass from another view by action link like this
#Html.ActionLink("Chose Car Type for Car", "Addcar", "Car", new { id = item.Ctid }, null)|
so what i want is to pass an attribute from two different views belong to 2 different controller and different action, how can i do that, i just need to make the attribute from one view appear in other under any type like viewbag or anything.
when you enter your URL ,you are trying to call a Controller . so you should catch the parameter as argument of the controller method , and then pass it to view with something like viewbag
in your car controller:
public ActionResult AddCar(int Id)
{
ViewBag.Id= Id;
return View();
}
in your view :
#{int id = ViewBag.Id; }<br />
#Html.TextBox("TextboxName",#id)
One simple example in your context-
public class HomeController : Controller
{
public string Index(string id, string name)
{
return "ID =" + id+"<br /> Name="+name;
}
}
else while returning view, you can do something like-
return View("Act", new { yourQueryString= "itsValueHere" });
And after that you can-
Textboxfor is binding data with Model. So you have to bind view with a model.
make AddCart action with id paramter, after that you can see when you pass the value that will appear in id parameter. After that load the model or make the model with the id and properties and pass to the view.
In the View
#Html.TextBox("TextboxName",ViewBag.YourProperty)
In the Controller
ViewBag.YourProperty= 'test';
You should pass the ID through the model defined for this view.
Another way is to use Html.TextBox() and provide the value manually using #Request.RequestContext.RouteData.Values["id"]
So in the same controller I have a Login Action method like this:
public ActionResult Login()
{
LoginModel model = this.LoginManager.LoadLoginPageData();
this.ForgotPasswordMethod = model.ForgotPasswordMethod;
return View(model);
}
Notice I set a variable there: ForgotPasswordMethod
So now when there on that page if they click on a link, it call another action result in the same controller class like this:
public ActionResult ForgotPassword()
{
if (!string.IsNullOrWhiteSpace(this.ForgotPasswordMethod) && this.ForgotPasswordMethod.Trim().ToUpper() == "TASKS")
return View();
return null; //todo change later.
}
Notice I tried to read the value of ForgotPasswordMethod , but it was NULL but it is NOT null when I am in the Login() method. So what should I do?
ASP.NET MVC was designed to return back to a cleaner, more straightforward web world built on HTTP, which is stateless, meaning that there is no "memory" of what has previously occurred unless you specifically use a technique that ensures otherwise.
As a result, whatever state you set via one ActionResult will no longer be the same state that exists when another ActionResult is invoked.
How do you "fix" this? You have a variety of options, depending on what your needs are:
Render the value to the client and post the value back to your second ActionResult method.
Store the value as a header and check that header.
Store the value in a cookie and check the cookie.
Store the value in session.
Store the value in a database.
Store the value in a static dictionary.
what if you store forgotpasswordmethod in Viewbag like
public ActionResult Login()
{
LoginModel model = this.LoginManager.LoadLoginPageData();
Viewbag.ForgotPasswordMethod = model.ForgotPasswordMethod;
return View(model);
}
then in the link of your page you can pass the value from the ViewBag
<a href=#Url.Action("ForgotPassword", "Name of your Controller", new { methodName = ViewBag.ForgotPasswordMethod })>Forgot Password</a>
Change your forgotpassword to
public ActionResult ForgotPassword(string methodName)
{
if (!string.IsNullOrWhiteSpace(methodName) && methodName.Trim().ToUpper() == "TASKS")
return View();
return null; //todo change later.
}
I m modifying an existing code.
From one action I use RedirectToAction to transfer execution control to another action. I need to pass Model with RedirectToAction as well. My idea is it can't be done directly by passing Model to 2nd action without using Session or tempData. But still want to ask is there a technique to pass model with RedirectToAction ? I don't want to put Model in Session or TempData.
Thanks
You can try something like that, but it doesn't feel like a natural action:
public ActionResult Index()
{
return RedirectToAction("AnotherAction", new
{
Parameter1 = Parameter1,
Parameter2 = Parameter2,
});
}
[HttpGet]
public ActionResult AnotherAction(ModelClass model)
{
//model.Parameter1
//model.Parameter2
return View(model);
}
I would expect the code of the previous answer to throw an exception when attempting to implicitly convert an object of parameter1 and parameter2 to ModelClass.
That being said, The best way is to just pass the id of the entity to your new action, then access your repository to initialize your model with the id that has been passed. Lets assume you have initialized a user with a UserID property.
return RedirectToAction("NextAction", new { id = user.UserID });
Then in NextAction just initialize the model with the passed id
I have a mvc application. I want to post 2 string from my controller to another controller in controller. Is it possible?
You can target any controller action from your form input in order to POST a string. If you want to pass a string or object from controller to controller you can use sessions or alternatively a database.
Additionally if you want to pass strings or objects between action methods in the same controller you can use the TempData collection as well.
Yes, You can use TempData for that like...
public ActionResult Sample1()
{
TempData["Test"] = "Test1"
return RedirectToAction("Sample2");
}
public ActionResult Sample2()
{
var test= TempData["Test"] as string
return View( test);
}
So here's what I'd like to do. I have a page at /Admin/Vendors/Index. If a user is in a certain role and they go to this page, I want them to be redirected to another view that only shows them certain data.
Here's what I have:
public ActionResult Index()
{
if (User.IsInRole("Special User"))
{
SpecialIndex();
}
return View();
}
public ActionResult SpecialIndex()
{
var viewModel = GetSpecialData();
return View(viewModel);
}
So I thought that if the user was in the role and it calls the SpecialIndex method, it would call the method and send the user to the SpecialIndex view.
It does call the SpecialIndex method, but when I call return View(viewModel), it just goes back to the Index method, and the user is shown the Index view instead of SpecialIndex.
How can I fix that?
You should use the View() constructor that gets a view name in which case you can specifically say which view you want to return, or use the RedirectToAction method to redirect to the SpecialIndex action. See documentation public ActionResult Index()
{
if (User.IsInRole("Special User"))
{
return this.RedirectToAction("SpecialIndex");
}
return View();
}
Check User.IsInRole("Special User") it will solve your problem. Either way, you are returning a view for it.