straight forward question , can't seem to get my viewBag value to display in a view that the user is directed to after completing a form.
Please advise..thanks
My Index ActionResult simple returns model data..
public ActionResult Index()
{
var source = _repository.GetByUserID(_applicationUser.ID);
var model = new RefModel
{
test1 = source.test1,
};
return View(model);
}
My Get Edit" ActionResult , simply uses the same model data as Index.
My Post "Edit" ActionResult, assigns the new values if any to the model and redirects to the Index page, but Index page does not display ViewBag value ??
[HttpPost]
public ActionResult Edit(RefModell model)
{
if (ModelState.IsValid)
{
var source = _repository.GetByUserID(_applicationUser.ID);
if (source == null) return View(model);
source.test1 = model.test1;
_uow.SaveChanges();
#ViewBag.Message = "Profile Updated Successfully";
return RedirectToAction("Index");
}
return View(model);
}
And in my Index view...
#if(#ViewBag.Message != null)
{
<div>
<button type="button">#ViewBag.Message</button>
</div>
}
ViewBag only lives for the current request. In your case you are redirecting, so everything you might have stored in the ViewBag will die along wit the current request. Use ViewBag, only if you render a view, not if you intend to redirect.
Use TempData instead:
TempData["Message"] = "Profile Updated Successfully";
return RedirectToAction("Index");
and then in your view:
#if (TempData["Message"] != null)
{
<div>
<button type="button">#TempData["Message"]</button>
</div>
}
Behind the scenes, TempData will use Session but it will automatically evict the record once you read from it. So it's basically used for short-living, one-redirect persistence storage.
Alternatively you could pass it as query string parameter if you don't want to rely on sessions (which is probably what I would do).
RedirectToAction causes an HTTP 302 response, which makes the client make another call to the server and request a new page.
You should be returning a view instead of redirecting.
The RedirectToAction(msdn) instructs your browser to make a new request.
So your server will be called again but it will be a new request with a blank viewbag and all
You could do a sort of internal redirect by just calling the index method, this way the viewbag will still have its data.
Edit : you'll also have to modify your index method or your View(model) line will try to render the edit. Full code below
public ActionResult Index()
{
var source = _repository.GetByUserID(_applicationUser.ID);
var model = new RefModel
{
test1 = source.test1,
};
return View("Index",model);
}
[HttpPost]
public ActionResult Edit(RefModell model)
{
if (ModelState.IsValid)
{
var source = _repository.GetByUserID(_applicationUser.ID);
if (source == null) return View(model);
source.test1 = model.test1;
_uow.SaveChanges();
#ViewBag.Message = "Profile Updated Successfully";
return Index();
}
return View(model);
}
You can try this way also
Controller
public ActionResult Test()
{
ViewBag.controllerValue= "testvalue";
..................
}
View -
define top of razor page
#{string testvalue= (string)ViewBag.controllerValue;}
$(function () {
var val= '#testvalue';
});
Related
I am having an issue with the controller action where I upload an image and the initial POSTed result is displayed in the view without issue. The image is shown normally.
If I refresh or change pages and return, the image will no longer display.
I have checked what the controller action is returning, and it does indeed return the content to the view.
This is the IMG tag that I'm using to call the controller action.
<img src="#Url.Action("Display", "Home")" />
This is the controller action:
public ActionResult Display()
{
var tempModel = new PartialModel();
tempModel = (PartialModel)Session["Model"];
var ImageUpload = tempModel.ProductImage;
if (ImageUpload != null)
{
byte[] content = new byte[ImageUpload.ContentLength];
ImageUpload.InputStream.Read(content, 0, ImageUpload.ContentLength);
return File(content, ImageUpload.ContentType);
}
return null;
}
This is the create action that receives the POST values:
public ActionResult Create(PartialModelStore p)
{
Session.RemoveAll();
//m.initializeModelLists(p);
Session["Model"] = p;
//return View("Index", p);
return RedirectToAction("Index");
}
I'm using the Session to store values because this is just a mock up application.
I have looked into using Data URI but I need to support older browsers, thus I'm using this method.
Is it possible to set the ViewBag before I call a redirection?
I want something like:
#ViewBag.Message="MyMessage";
RedirectToAction("MyAction");
When you use redirection, you shall not use ViewBag, but TempData
public ActionResult Action1 () {
TempData["shortMessage"] = "MyMessage";
return RedirectToAction("Action2");
}
public ActionResult Action2 () {
//now I can populate my ViewBag (if I want to) with the TempData["shortMessage"] content
ViewBag.Message = TempData["shortMessage"].ToString();
return View();
}
You can use the TempData in this situation.
Here is some explanation for the ViewBag, ViewData and TempData.
I did like this..and its working for me...
here I'm changing password and on success I want to set success message to viewbag to display on view..
public ActionResult ChangePass()
{
ChangePassword CP = new ChangePassword();
if (TempData["status"] != null)
{
ViewBag.Status = "Success";
TempData.Remove("status");
}
return View(CP);
}
[HttpPost]
public ActionResult ChangePass(ChangePassword obj)
{
if (ModelState.IsValid)
{
int pid = Session.GetDataFromSession<int>("ssnPersonnelID");
PersonnelMaster PM = db.PersonnelMasters.SingleOrDefault(x => x.PersonnelID == pid);
PM.Password = obj.NewPassword;
PM.Mdate = DateTime.Now;
db.SaveChanges();
TempData["status"] = "Success";
return RedirectToAction("ChangePass");
}
return View(obj);
}
Taken from here
Summary
The ViewData and ViewBag objects give you ways to access those extra pieces of data that go alongside your model, however for more complex data, you can move up to the ViewModel. TempData, on the other hand, is geared specifically for working with data on HTTP redirects, so remember to be cautious when using TempData.
Or you can use Session for alternative:
Session["message"] = "MyMessage";
RedirectToAction("MyAction");
and then call it whenever you need.
UPDATE
Also, as what #James said in his comment, it would be safe to nullify or clear the value of that specific session after you use it in order to avoid unwanted junk data or outdated value.
I use Directoryservices for login in My page. I need to pass the username to my masterpage to display the username in all the pages.
I got the username and stored it in a ViewData. How to pass the viewdata value in masterpage.
My code :
[HttpPost]
public ActionResult Index(LoginModels model)
{
if (ModelState.IsValid)
{
string DisplayUserName = string.Empty;
string LoginUser = model.Userid;
string LoginPassword = model.Password;
string name = model.UserName
if (ValidateActiveDirectoryLogin(LoginUser, LoginPassword, out DisplayUserName) == true)
{
model.UserName = DisplayUserName;
ViewData["UserName"] = "Welcome" + DisplayUserName;
return RedirectToAction("Index", "MPP", new { UserID = LoginUser });
}
else
{
ModelState.AddModelError("","Invalid Username or Password");
}
}
return View();
}
In Layout page :
#{ #ViewData["UserName"] }
I tried the following way to display the Username. But it throws nullexception.
EDIT :
#foreach (var m in IEnumerable<SampleECommerce.Models.LoginModels>)ViewData["UserName"])
{
#m.UserName
}
There are some misunderstandings, like if you set ViewData["UserName"] to a string value you get a IEnumerable<SampleECommerce.Models.LoginModels>. Here is another solution:
Put this to layout page:
<span>#{Html.RenderAction("actionname", "controllername");}</span>
And in related action:
public ActionResult actionname() {
string result = getusername();
return Content(result);
}
[NoneAction]
private string getusername(){
return (Membership.GetUser()!= null) ? Membership.GetUser().UserName : "Guest";
}
Try it without the extra #, i.e.
#{ ViewData["UserName"] }
Firs you need to change your syntax to:
#(ViewData["UserName"])
That's probably the best (of a bad bunch). Realistically you should be looking to push your user into the User property of your pages via the User property of your controllers (typically in an authorization attribute where, perhaps, you read a cookie) - that way you don't rely on type-unsafe ViewData and magic strings for something that you're going to be using on every page.
But anyway... if the view is rendering because of the last return View(); line then what you're trying to do will work if you change your syntax as I've shown.
If not, and it's when you do return RedirectToAction("Index", "MPP", new { UserID = LoginUser }); then you need to push the UserName into TempData and then read it back at the start of the Index action on your MPP controller:
So:
TempData["UserName"] = "Welcome " + DisplayUserName;
return RedirectToAction("Index", "MPP", new { UserID = LoginUser });
And then at the start of your Index method you need to pull the value back out of TempData:
public class MPPController {
public ActionResult Index(){
ViewData["UserName"] = TempData["UserName"];
}
}
Why do you have to do this? Because RedirectToAction doesn't render a page - it tells the client to make a different request to a new Url - thus any ViewData or model or whatever is thrown away as far as the server is concerned. TempData is there to provide temporary storage between two successive requests only - thus it works well for the RedirectToAction scenario.
Like I say though - this really is a poor way to persist your user information from controller to view and you should seriously rethink it as a matter of urgency.
in layout page:
<span>#{Html.RenderAction("actionname", "controllername");}</span>
in controller store a session variable
[HttpPost]
public ActionResult Index(LoginModels model)
{
Session["username"] = model.UserName;
//remaining code
}
add one more function
public ActionResult actionname() {
return Content(Session["username"]);
}
so here we dont need additional functions.
I am new to MVC4. Here I added the ModelState.AddModelError message to display when the delete operation is not possible.
<td>
<a id="aaa" href="#Url.Action("Delete", "Shopping", new { id = Request.QueryString["UserID"], productid = item.ProductID })" style="text-decoration:none">
<img alt="removeitem" style="vertical-align: middle;" height="17px" src="~/Images/remove.png" title="remove" id="imgRemove" />
</a>
#Html.ValidationMessage("CustomError")
</td>
#Html.ValidationSummary(true)
In my controller
public ActionResult Delete(string id, string productid)
{
int records = DeleteItem(id,productid);
if (records > 0)
{
ModelState.AddModelError("CustomError", "The item is removed from your cart");
return RedirectToAction("Index1", "Shopping");
}
else
{
ModelState.AddModelError(string.Empty,"The item cannot be removed");
return View("Index1");
}
}
Here I didnt pass any of the model item in the View to check for the item in Model and I couldnt get the ModelState error message .. Any suggestions
The ModelState is created at each request so you should use TempData.
public ActionResult Delete(string id, string productid)
{
int records = DeleteItem(id,productid);
if (records > 0)
{
// since you are redirecting store the error message in TempData
TempData["CustomError"] = "The item is removed from your cart";
return RedirectToAction("Index1", "Shopping");
}
else
{
ModelState.AddModelError(string.Empty,"The item cannot be removed");
return View("Index1");
}
}
public ActionResult Index1()
{
// check if TempData contains some error message and if yes add to the model state.
if(TempData["CustomError"] != null)
{
ModelState.AddModelError(string.Empty, TempData["CustomError"].ToString());
}
return View();
}
RedirectToAction will clear ModelState. You must return a view in order to use this data. Therefore, the first if case won't work. Also, ensure that you have a control in your view (like ValidationSummary) which displays the error... this could be the problem in the second case.
The RedirectToAction method returns 302 which causes the client to be redirected. Because of this the ModelState is lost as the redirect is a new request. You could however, use the TempData property which allows you to store a temporary piece of data that is unique to the session. You could then check for this TempData on the other controller and add a ModelState error in that method.
I have a form which a user can fill in x times with the data they want too. The form is posted to the following Action.
[HttpPost]
public ActionResult Manage(ProductOptionModel DataToAdd)
{
if (!ModelState.IsValid)
{
return View(DataToAdd);
}
var ProdServ = new ProductService();
if (DataToAdd.ID != 0)
{
//Edit Mode.
DataToAdd = ProdServ.EditProductOption(DataToAdd);
ViewData["Message"] = "Option Changes Made";
}else
{
//Add
DataToAdd = ProdServ.AddProductOption(DataToAdd);
ViewData["Message"] = "New Option Added";
}
var RetModel = new ProductOptionModel() {ProductID = DataToAdd.ProductID};
return View(RetModel);
}
So at the bottom I blank the model (Leaving just the required field) and then return to the view. However the view holds the data from the previously submitted form.
Any ideas why? I have debugged the code and checked that the RetModel variable is empty.
Html helpers work this way when a view is returned on HTTP POSTs. They prefer post data over model values.
Use Post/Redirect/Get
That's why I suggest you use the Post/Redirect/Get pattern that's very well supported in Asp.net MVC. Your controller actions should redirect to some GET action after POST has successfully completed as it is in your case.
public ActionResult Process()
{
return View(new Data { Id = -1 });
}
[HttpPost]
public ActionResult Process(Data data)
{
if (!this.ModelState.IsValid)
{
return View(data);
}
new MyService().ProcessData(data);
return RedirectToAction("Process");
}
And if you display all previously entered data you can provide those in in the GET action or transfer them from POST to GET action using TempData dictionary.
This is because the build in input helpers will look at the posted data first and use those values if they exist. Then it will look at the model.