When I register a user, it redirects to the Manage controller (where the user profile is).
return RedirectToAction("Index", "Manage", new { id = user.Id });
and in the Manage Controller
[Route("")]
[Route("{id}")]
public ActionResult Index(string id)
{
if (id == null)
{
id = User.Identity.GetUserId();
}
var user = UserManager.FindById(id);
return View(user);
}
I need to send a string message of "newUser", so I can display a modal message the first time the user makes the account and is redirected to their profile.
My question is how exactly? I tried to send a string parameter like this:
return RedirectToAction("Index", "Manage", new { id = user.Id, msg = "newUser" });
and change the Index constructor to Index(string id, string msg) but for some reason both the id and msg get the value of the user id.
Anyone know a solution to this?
Manage / Index
public ActionResult Index(string id)
{
if (id == null)
{
id = User.Identity.GetUserId();
}
var user = UserManager.FindById(id);
return View(user);
}
You can make use of TempData for passing data from one controller to anhother.
Example :
public ActionResult Index()
{
var model = new Review()
{
Body = "Start",
Rating=5
};
TempData["ModelName"] = model;
return RedirectToAction("About");
}
public ActionResult About()
{
var model= TempData["ModelName"];
return View(model);
}
Refer this link for more info : https://www.codeproject.com/articles/476967/what-is-viewdata-viewbag-and-tempdata-mvc-option
You code syntax is almost correct except you are missing " in msg = "newUser
You could have use TempData for sending data from controller to controller.
For more reference :
http://www.tutorialsteacher.com/mvc/tempdata-in-asp.net-mvc
Related
I want to know, there is any technique so we can pass Model as a parameter in RedirectToAction
For Example:
public class Student{
public int Id{get;set;}
public string Name{get;set;}
}
Controller
public class StudentController : Controller
{
public ActionResult FillStudent()
{
return View();
}
[HttpPost]
public ActionResult FillStudent(Student student1)
{
return RedirectToAction("GetStudent","Student",new{student=student1});
}
public ActionResult GetStudent(Student student)
{
return View();
}
}
My Question - Can I pass student model in RedirectToAction?
Using TempData
Represents a set of data that persists only from one request to the
next
[HttpPost]
public ActionResult FillStudent(Student student1)
{
TempData["student"]= new Student();
return RedirectToAction("GetStudent","Student");
}
[HttpGet]
public ActionResult GetStudent(Student passedStd)
{
Student std=(Student)TempData["student"];
return View();
}
Alternative way
Pass the data using Query string
return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"});
This will generate a GET Request like Student/GetStudent?Name=John & Class=clsz
Ensure the method you want to redirect to is decorated with [HttpGet] as
the above RedirectToAction will issue GET Request with http status
code 302 Found (common way of performing url redirect)
Just call the action no need for redirect to action or the new keyword for model.
[HttpPost]
public ActionResult FillStudent(Student student1)
{
return GetStudent(student1); //this will also work
}
public ActionResult GetStudent(Student student)
{
return View(student);
}
Yes you can pass the model that you have shown using
return RedirectToAction("GetStudent", "Student", student1 );
assuming student1 is an instance of Student
which will generate the following url (assuming your using the default routes and the value of student1 are ID=4 and Name="Amit")
.../Student/GetStudent/4?Name=Amit
Internally the RedirectToAction() method builds a RouteValueDictionary by using the .ToString() value of each property in the model. However, binding will only work if all the properties in the model are simple properties and it fails if any properties are complex objects or collections because the method does not use recursion. If for example, Student contained a property List<string> Subjects, then that property would result in a query string value of
....&Subjects=System.Collections.Generic.List'1[System.String]
and binding would fail and that property would be null
[HttpPost]
public async Task<ActionResult> Capture(string imageData)
{
if (imageData.Length > 0)
{
var imageBytes = Convert.FromBase64String(imageData);
using (var stream = new MemoryStream(imageBytes))
{
var result = (JsonResult)await IdentifyFace(stream);
var serializer = new JavaScriptSerializer();
var faceRecon = serializer.Deserialize<FaceIdentity>(serializer.Serialize(result.Data));
if (faceRecon.Success) return RedirectToAction("Index", "Auth", new { param = serializer.Serialize(result.Data) });
}
}
return Json(new { success = false, responseText = "Der opstod en fejl - Intet billede, manglede data." }, JsonRequestBehavior.AllowGet);
}
// GET: Auth
[HttpGet]
public ActionResult Index(string param)
{
var serializer = new JavaScriptSerializer();
var faceRecon = serializer.Deserialize<FaceIdentity>(param);
return View(faceRecon);
}
[NonAction]
private ActionResult CRUD(someModel entity)
{
try
{
//you business logic here
return View(entity);
}
catch (Exception exp)
{
ModelState.AddModelError("", exp.InnerException.Message);
Response.StatusCode = 350;
return someerrohandilingactionresult(entity, actionType);
}
//Retrun appropriate message or redirect to proper action
return RedirectToAction("Index");
}
i did find something like this, helps get rid of hardcoded tempdata tags
public class AccountController : Controller
{
[HttpGet]
public ActionResult Index(IndexPresentationModel model)
{
return View(model);
}
[HttpPost]
public ActionResult Save(SaveUpdateModel model)
{
// save the information
var presentationModel = new IndexPresentationModel();
presentationModel.Message = model.Message;
return this.RedirectToAction(c => c.Index(presentationModel));
}
}
i can't figure out why the username in the controller always returned as null. When i clicked the button, the button works fine and return the member's username.
http://localhost:50555/Account/UserDetails/member1
In my view
<button data-get="/Account/UserDetails/#m.Username">Details</button>
In controller
[Authorize(Roles = "Supervisor, Admin")]
public ActionResult UserDetails(string username)
{
var model = db.Members.Find(username);
if (model == null)
{
return RedirectToAction("ManageUsers");
}
return View(model);
}
You are not taking the value of username from the querystring. You are expecting a route parameter. So you will have to specify the route parameter as shown below:
[Authorize(Roles = "Supervisor, Admin")]
[HttpGet("{username}")]
public ActionResult UserDetails(string username)
{
var model = db.Members.Find(username);
if (model == null)
{
return RedirectToAction("ManageUsers");
}
return View(model);
}
Refer this documentation for more details.
this is the controller
public ActionResult Test() {
#ViewBag.TheMessageIs = "this is the message";
return RedirectToAction("Details", new { id = theId});
}
on the view of Action Named Details I will check if it has the ViewBag to show and show it:
#{
if(ViewBag.TheMessageIs != null){
#ViewBag.TheMessageIs
}
}
but here the redirection is working fine to the page, it's not show the message I have stored in ViewBag.TheMessageIs
thanks
Basically what you're doing is invoking the method Details from your Index method and since you're already overloading your Details action with an id, pass it the message as well:
public ActionResult Index()
{
//ViewBag.TheMessageIs = "this is the message";
return RedirectToAction("Details", new { id = 1, TheMessageIs = "this is the message" });
}
public ActionResult Details(int id, string TheMessageIs)
{
ViewBag.TheMessageIs = TheMessageIs;
return View();
}
Then in the Details view you can access the property like this:
#ViewBag.TheMessageIs
public ActionResult Test() {
TempData["shortMessage"] = "MyMessage";
return RedirectToAction("Details", new { id = theId});
}
public ActionResult Details {
//now I can populate my ViewBag (if I want to) with the TempData["shortMessage"] content
ViewBag.TheMessageIs = TempData["shortMessage"].ToString();
return View();
}
You have to do it like this since the viewbag looses its value when you redirect to another active / view
So, I am experimenting with ASP.NET MVC and I have the following code:
public class TrollController : Controller
{
public ActionResult Index()
{
var trollModel = new TrollModel()
{
Name = "Default Troll",
Age = "666"
};
return View(trollModel);
}
[HttpPost]
public ActionResult Index(TrollModel trollModel)
{
return View(trollModel);
}
public ActionResult CreateNew()
{
return View();
}
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
return RedirectToAction("Index");
}
}
The idea is to have an index page which shows the age of our troll as well as his name.
There's an action that allows us to create a troll, and after creating it we should get back to the index page but this time with our data, instead of the default one.
Is there a way to pass the TrollModel CreateNew(TrollModel trollModel) is receiving to Index(TrollModel trollModel)? If yes, how?
The best approach would be to persist the troll somewhere on the server (database?) and then pass only the id to the index action when redirecting so that it can fetch it back. Another possibility is to use TempData or Session:
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
TempData["troll"] = trollModel;
return RedirectToAction("Index");
}
public ActionResult Index()
{
var trollModel = TempData["troll"] as TrollModel;
if (trollModel == null)
{
trollModel = new TrollModel
{
Name = "Default Troll",
Age = "666"
};
}
return View(trollModel);
}
TempData will survive only a single redirect and be automatically evicted on the subsequent request whereas Session will be persistent across all HTTP requests for the session.
Yet another possibility consists into passing all the properties of the troll object as query string arguments when redirecting:
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
return RedirectToAction("Index", new
{
Age = trollModel.Age,
Name = trollModel.Name
});
}
public ActionResult Index(TrollModel trollModel)
{
if (trollModel == null)
{
trollModel = new TrollModel
{
Name = "Default Troll",
Age = "666"
};
}
return View(trollModel);
}
Now you might need to rename the Index POST action as you cannot have two methods with the same name and arguments:
[HttpPost]
[ActionName("Index")]
public ActionResult HandleATroll(TrollModel trollModel)
{
return View(trollModel);
}
In CreateNew there must be some kind of persistence e.g. the troll might be saved in database. It must also have some kind of ID. So the Index method can be changed to
public ActionResult Index(string id)
{
TrollModel trollModel;
if (string.IsNullOrEmpty(id))
{
trollModel = new TrollModel()
{
Name = "Default Troll",
Age = "666"
};
}
else
{
trollModel = GetFromPersisted(id);
}
return View(trollModel);
}
and in the CreateNew
[HttpPost]
public ActionResult CreateNew(TrollModel trollModel)
{
return RedirectToAction("Index", new {id = "theNewId"});
}
Here is an example method I have that deletes a record from my app:
[Authorize(Roles = "news-admin")]
public ActionResult Delete(int id)
{
var ArticleToDelete = (from a in _db.ArticleSet where a.storyId == id select a).FirstOrDefault();
_db.DeleteObject(ArticleToDelete);
_db.SaveChanges();
return RedirectToAction("Index");
}
What I would like to do is show a message on the Index view that says something like: "Lorem ipsum article has been deleted" how would I do this? Thanks
Here is my current Index method, just in case:
// INDEX
[HandleError]
public ActionResult Index(string query, int? page)
{
// build the query
var ArticleQuery = from a in _db.ArticleSet select a;
// check if their is a query
if (!string.IsNullOrEmpty(query))
{
ArticleQuery = ArticleQuery.Where(a => a.headline.Contains(query));
//msp 2011-01-13 You need to send the query string to the View using ViewData
ViewData["query"] = query;
}
// orders the articles by newest first
var OrderedArticles = ArticleQuery.OrderByDescending(a => a.posted);
// takes the ordered articles and paginates them using the PaginatedList class with 4 per page
var PaginatedArticles = new PaginatedList<Article>(OrderedArticles, page ?? 0, 4);
// return the paginated articles to the view
return View(PaginatedArticles);
}
One way would be to use TempData:
[Authorize(Roles = "news-admin")]
public ActionResult Delete(int id)
{
var ArticleToDelete = (from a in _db.ArticleSet where a.storyId == id select a).FirstOrDefault();
_db.DeleteObject(ArticleToDelete);
_db.SaveChanges();
TempData["message"] = ""Lorem ipsum article has been deleted";
return RedirectToAction("Index");
}
and inside the Index action you could fetch this message from TempData and make use of it. For example you could pass it as a property of your view model which will be passed to the view so that it can show it:
public ActionResult Index()
{
var message = TempData["message"];
// TODO: do something with the message like pass to the view
}
UPDATE:
Example:
public class MyViewModel
{
public string Message { get; set; }
}
and then:
public ActionResult Index()
{
var model = new MyViewModel
{
Message = TempData["message"] as string;
};
return View(model);
}
and inside the strongly typed view:
<div><%: Model.Message %></div>