Answered by:
Show a message in a div on parent view

Question
-
User1052662409 posted
Hi All,
I have a view named Index.
In this I am populating a modal. I am submitting this modal with some value. There is no issue its working fine and saving my values into database.
Now I just want to show a thank you text after submitting the modal. But as I go further it asks for a view. For sample see the below code.
if (true) { TempData["SampleText"] = "<div class=\"alert alert-success\">Success</div>"; return View(); } else { TempData["SampleText"] = "<div class=\"alert alert-success\">Error</div>"; return View(); }
On my Index view I have div
<div id="divmsg"> @Html.Raw(TempData["SampleText"] + "")</div>
I guess as I am returning a view(), that is why it is asking for view. Then what should I return for my desired message?
Please suggest
Tuesday, August 6, 2019 5:24 PM
Answers
-
User475983607 posted
demoninside9
the view code is too long and I don't want to waste your valuable time.But you are wasting our time by making us guess why you are unable to write TempData to a View.
demoninside9
I just want to know is there any way I can show the TempData["ThankYouText"] data on message box?[HttpGet] public ActionResult Index() { TempData["ThankYouText"] = "Hello World"; return View(); }
@{ ViewBag.Title = "Index"; } <h2>Index</h2> @section Scripts { <script> alert("@TempData["ThankYouText"].ToString()"); </script> }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 6, 2019 8:07 PM
All replies
-
User475983607 posted
It is impossible to answer your question give the code snippet.
demoninside9
In this I am populating a modal. I am submitting this modal with some value. There is no issue its working fine and saving my values into database.
Now I just want to show a thank you text after submitting the modal. But as I go further it asks for a view. For sample see the below code.
Are you submitting a form post? AJAX? Is there anyway you can share all the relevant code so we are not guessing? We need the Actions, Views, and Models involved.
Also please explain the troubleshooting steps performed up to this point. Are you running your code through the Visual Studio debugger? Are yo u using Dev tools (F12)?
Tuesday, August 6, 2019 5:32 PM -
User1052662409 posted
mgebhard
We need the Actions, Views, and Models involved.Respected Sir,
My Action is having a long (with many if else conditions) code and working fine. I just need to show a message.
For reference I just putting my action (some code).
[HttpPost] public ActionResult Create(UserLeaveData ULD, FormCollection form, HttpPostedFileBase file) { try { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); sendemailmethod obj = new sendemailmethod(); if ((Session["AdminType"] + "" == "m") || (Session["_ROLE"] + "" == "HR")) { } else { if ((Convert.ToBoolean(Request.Form["chkdisclaimer"].ToString()) == false) && (Convert.ToBoolean(Request.Form["chktaken"].ToString()) == false)) { if (Session["leaveapprovalprocess"] + "" == "A") { string _msg1 = App_GlobalResources.Resource.a_e_leave_msg1; TempData["ThankYouText"] = _msg1; return View(); } } } System.DateTime sdatecheck = Convert.ToDateTime("" + DateTime.ParseExact(Request.Form["txtleavestart"].ToString(), "dd-MMM-yyyy", CultureInfo.InvariantCulture) .ToString("MM/dd/yyyy", CultureInfo.InvariantCulture) + ""); System.DateTime edatecheck = Convert.ToDateTime("" + DateTime.ParseExact(Request.Form["txtleaveend"].ToString(), "dd-MMM-yyyy", CultureInfo.InvariantCulture) .ToString("MM/dd/yyyy", CultureInfo.InvariantCulture) + ""); bool _leavemultiple = false; if (sdatecheck.Year != edatecheck.Year) { _leavemultiple = true; } DataTable dtdupleave = objDB.duplicateleave(Convert.ToInt32(Session["adminID"].ToString()), sdatecheck.ToString(), edatecheck.ToString()); if (dtdupleave.Rows.Count > 0) { if (Convert.ToBoolean(Session["Leave-Half"] + "")) { if (dtdupleave.Rows[0]["_halfday"] + "" == "") { TempData["ThankYouText"] = "Leave apply date already exists in our system."; return View(); } else { if (Request.Form["ddlHalfDay"].ToString() != "") { if (dtdupleave.Rows[0]["_halfday"] + "" == Request.Form["halfdayselect"].ToString()) { TempData["ThankYouText"] = "Leave apply date already exists in our system."; return View(); } } else { TempData["ThankYouText"] = "Leave apply date already exists in our system."; return View(); } } } else { TempData["ThankYouText"] = "<div class=\"alert alert-success\">Leave apply date already exists in our system.</div>"; return View(); } } } catch (Exception ex) { TempData["ThankYouText"] = "Error"; } return View(); }
I just show TempData["ThankYouText"] on the same Index view. I don't want a view for "Create". (As popup modal is working for it).
Otherwise it shows error. I need TempData["ThankYouText"] instead a view for Create.
The view 'Create' or its master was not found or no view engine supports the searched locations. The following locations were searched:
Tuesday, August 6, 2019 5:49 PM -
User1052662409 posted
Are you submitting a form post?Yes
AJAX?No
Tuesday, August 6, 2019 5:59 PM -
User475983607 posted
Can you explain how a forum member could possible know that the action has 5 different TempData["ThankYouText"] assignments and each within in a different condition?
But most importunately there is not a single line of code like below. It seems the code is not in the action so I'm not sure how you expect it to show in the View,
TempData["SampleText"] = "<div class=\"alert alert-success\">Error</div>";
And... This error...
The view 'Create' or its master was not found or no view engine supports the searched locations. The following locations were searched:
Means the View does not exist. I can only guess there a form action is incorrect or a link. Still you have not posted enough source code to reproduce this issue. Please debug your code.
Tuesday, August 6, 2019 6:06 PM -
User1052662409 posted
TempData["SampleText"]
Really very sorry just make it a meaningful I changed the name to TempData["ThankYouText"] but did not post the same
Sorry again.
Tuesday, August 6, 2019 6:08 PM -
User475983607 posted
demoninside9
Really very sorry just make it a meaningful I changed the name to TempData["ThankYouText"] but did not post the same
Sorry again.
TempData["ThankYouText"] has 5 possible values. I have no idea which one you expect. Plus we still do not have the View.
Please set a break point and single step through your code. It should be very easy to find the bug.
Tuesday, August 6, 2019 6:13 PM -
User1052662409 posted
Please set a break point and single step through your code.Okay Fine Sir.
TempData["ThankYouText"] has 5 possible values. I have no idea which one you expect.Does it matter? May be my question is foolish, but I just want to show a messagebox / div/<span> with whatever it is in TempData["ThankYouText"]
How can I achieve to show a message on post method?
Anyway if you did not understand my question that is my inability to make it clear.
Thanks
Tuesday, August 6, 2019 6:22 PM -
User475983607 posted
demoninside9
Anyway if you did not understand my question that is my inability to make it clear.I understand the question but you did not shared all the relevant code. I'm not sure why we must ask for this basic information on most of your posts.
Anyway, the first thing I would check is the View to make sure TempData["ThankYouText"] exists and the View markup is valid. You have not provided the View even after asking several times.
The next troubleshooting step is placing a break point at the top of the action and single step through the code until you find where TempData["ThankYouText"] is assigned.
As written, it is possible that TempData["ThankYouText"] is never set due to the row count being zero.
if (dtdupleave.Rows.Count > 0) {
Anyway, you have a lot of experience in these forums and should be able to do basic troubleshooting on your own.
Lastly, you posted an error message that indicates you forgot to create the View or there is a typo somewhere. I'm not sure if you are still getting the error message.
Tuesday, August 6, 2019 7:20 PM -
User1052662409 posted
I understand the question but you did not shared all the relevant codeSir, the view code is too long and I don't want to waste your valuable time.
you posted an error message that indicates you forgot to create the ViewAs I already told I don't want to a new view I just want to show the TempData["ThankYouText"] some where in any message box (like popup, alert());
I need a basic Idea that without view I want to show a message on post method. Can we have see TempData["ThankYouText"] data somewhere on my running page?
Lets say it this way. In If else statement If i get into if and there is I have declared a TempData["ThankYouText"] variable. I don't want to get go ahead I just need a break here and want to show what it in TempData["ThankYouText"].
you have a lot of experience in these forums and should be able to do basic troubleshooting on your own.Yes, I do. I am not saying that I am getting an error (Yes of course if you don't have a view you will get surely get error ). I just want to know is there any way I can show the TempData["ThankYouText"] data on message box?
Tuesday, August 6, 2019 7:41 PM -
User475983607 posted
demoninside9
the view code is too long and I don't want to waste your valuable time.But you are wasting our time by making us guess why you are unable to write TempData to a View.
demoninside9
I just want to know is there any way I can show the TempData["ThankYouText"] data on message box?[HttpGet] public ActionResult Index() { TempData["ThankYouText"] = "Hello World"; return View(); }
@{ ViewBag.Title = "Index"; } <h2>Index</h2> @section Scripts { <script> alert("@TempData["ThankYouText"].ToString()"); </script> }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 6, 2019 8:07 PM -
User1052662409 posted
mgebhard
[HttpGet] public ActionResult Index() { TempData["ThankYouText"] = "Hello World"; return View(); }
@{ ViewBag.Title = "Index"; } <h2>Index</h2> @section Scripts { <script> alert("@TempData["ThankYouText"].ToString()"); </script> }
Sir this is what I was trying to say but many not be as much elaborated as others could understand. That is why I don't want to put the whole model, view controller.
As I said before
demoninisde9
I just want to show a messagebox / div/<span> with whatever it is in TempData["ThankYouText"]Thanks
Wednesday, August 7, 2019 9:38 AM