I have a cshtml page, with 3 text boxes and 3 dropdowns.
My idea is to have the user make a decision on the first question dropdown (YES/NO), and depending on this answer, populate the second text box, and enable the second dropdown (YES/NO), and same process for the third textbox.
I have the following at the moment:-
<script type="text/javascript">
$(document).ready(function () {
//disable the textboxes
$("#T_FirstQuestion").attr('disabled', true);
$("#T_SecondQuestion").attr('disabled', true);
$("#T_ThirdQuestion").attr('disabled', true);
//and the dropdowns intially
$("#SecondQuestYesNo").attr('disabled', true);
$("#ThirdQuestYesNo").attr('disabled', true);
$("#FirstQuestYesNo").change(function () {
val = $("#FirstQuestYesNo").val();
PostValue(val);
});
function PostValue(val) {
var url = "/Home/DecisionFirstQuest";
$("#T_SecondQuestion").attr('enabled', true);
$.ajax({
type: "POST",
url: url,
data: { value: val }
}).done(function (msg) {
alert("Data Saved: " + msg);
});
}
});
</script>
@using (Html.BeginForm("Decision", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td> <font face="Arial" size="2"><b>1</b></font>
</td>
<td>
@Html.TextBox("T_FirstQuestion", ViewData["T_FirstQuestion"], new { @class = "NormalTextBox" })
</td>
<td>
@Html.DropDownList("FirstQuestYesNo", ViewData["FirstQuestYesNoData"] as SelectList, new { @class = "normalDropdowns" })
</td>
</tr>
<tr>
<td> <font face="Arial" size="2"><b>1</b></font>
</td>
<td>
@Html.TextBox("T_SecondQuestion", ViewData["T_SecondQuestion"], new { @class = "NormalTextBox" })
</td>
<td>
@Html.DropDownList("SecondQuestYesNo", ViewData["SecondQuestYesNoData"] as SelectList, new { @class = "normalDropdowns" })
</td>
</tr>
<tr>
<td> <font face="Arial" size="2"><b>1</b></font>
</td>
<td>
@Html.TextBox("T_ThirdQuestion", ViewData["T_ThirdQuestion"], new { @class = "NormalTextBox" })
</td>
<td>
@Html.DropDownList("ThirdQuestYesNo", ViewData["ThirdQuestYesNoData"] as SelectList, new { @class = "normalDropdowns" })
</td>
</tr>
</table>
}
and my Controller is as follows :-
public ActionResult DecisionFirstQuest(string value)
{
string strMessage = "";
if (value == "Yes")
{
strMessage = "You have chosen YES!";
}
else
{
strMessage = "You have chosen NO!";
}
ViewData["T_SecondQuestion"] = strMessage;
return RedirectToAction("Decision");
}
public ActionResult DecisionSecondQuest(string value)
{
string strMessage = "";
if (value == "Yes")
{
strMessage = "You have chosen YES!";
}
else
{
strMessage = "You have chosen NO!";
}
ViewData["T_ThirdQuestion"] = strMessage;
return RedirectToAction("Decision");
}
public ActionResult Decision()
{
string FirstQuestYesNo = HttpContext.Request["FirstQuestYesNo"];
ViewData["T_FirstQuestion"] = "First Question Text";
var ddlYesNoData = new SelectList(new[]
{
new {ID="",Name="Please Select"},
new {ID="Yes",Name="Yes"},
new{ID="No",Name="No"},
},
"ID", "Name", 1);
if (!String.IsNullOrEmpty(FirstQuestYesNo))
ViewData["FirstQuestYesNoData"] = FirstQuestYesNo;
else
ViewData["FirstQuestYesNoData"] = "Yes";
ViewData["FirstQuestYesNoData"] = ddlYesNoData;
ViewData["SecondQuestYesNoData"] = ddlYesNoData;
ViewData["ThirdQuestYesNoData"] = ddlYesNoData;
return View();
}
I am managing to get the value of the first dropdown, and redirecting to the Decision action, however I am not getting the second question textbox filled up. Also I am getting like a popup with some HTML code, which I would like to avoid.
So basically my question is, how can I fill up the second text box, and after the user chooses the (YES/NO), then fill up the third textbox.
Also, am I using the correct approach or is there a better way to do this using MVC?
Thanks for your help and time!
-------------------UPDATE-------------------------------------------- I decided to go for a more easy example
<script type="text/javascript">
$(document).ready(function () {
$("#YesNo").change(function () {
val = $("#YesNo").val();
var url = "../Home/Decision";
$.post(url, { value: val});
});
});
</script>
@using (Html.BeginForm("Decision", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id="Decision" }))
{
@Html.DropDownList("YesNo", new List<SelectListItem>
{
new SelectListItem{ Text="Select", Value = "" },
new SelectListItem{ Text="Yes", Value = "Yes" },
new SelectListItem{ Text="No", Value = "No" }
})
string FirstQuestText = ViewBag.FirstQuestData;
@Html.TextBox("T_FirstQuestion", FirstQuestText, new { @class = "NormalTextBox" })
}
And the Controller Actions:-
[HttpPost]
public ActionResult Decision(string value)
{
string strMessage = "";
if (value == "Yes")
{
strMessage = "This is the Second Yes Question";
}
else
{
strMessage = "This is the Second No Question";
}
ViewBag.FirstQuestData = strMessage;
return View();
}
The problem now is that I am getting the ViewBag.FirstQuestData populating correctly, however it is not displayed in the @Html.TextBox
-----------------------------------JSON UPDATE--------------------------------------- cshtml
$("#YesNoQuest1").change(function () {
alert('change');
val = $("#YesNoQuest1").val();
var url = "../Home/Decisions1";
$.getJSON(url, function(data) {
alert(data.message);
});
controller
[HttpPost]
public JsonResult Decisions1(string value)
{
string strMessage = "";
if (value == "Yes")
{
strMessage = "This is the Second Yes Question";
}
else
{
strMessage = "This is the Second No Question";
}
return Json(new { message = strMessage }, JsonRequestBehavior.AllowGet);
}
Try returning string based data instead of Redirecting to Action as below:
[HttpPost]
public ActionResult Decision(string value)
{
string strMessage = "";
if (value == "Yes")
{
strMessage = "This is the Second Yes Question";
}
else
{
strMessage = "This is the Second No Question";
}
ViewBag.FirstQuestData = strMessage;
return Content(strMessage); //No need to return complete View
}
You can receive this message in the Ajax post call as below:
$("#YesNo").change(function () {
val = $("#YesNo").val();
var url = "../Home/Decision";
$.post(url, { value: val},function(data){
alert(data);
//Here you can right your logic to manipulate data
});
});
Hope this helps:
------- UPDATE to Use JSON data ------------------- Here is controller to return Json:
[HttpGet]
public ActionResult YourController()
{
//Do your Logic
return Json(new { message = "Data" }, JsonRequestBehavior.AllowGet);
}
$.getJSON("../YourController", function(data) {
alert(data.foo);
alert(data.baz);
});
If you use "Json result" from returning a response from Controller to View, mention datatype as 'json' in the Ajax Setting as
function PostValue(val) {
var url = "/Home/DecisionFirstQuest";
$("#T_SecondQuestion").attr('enabled', true);
$.ajax({
type: "POST",
url: url,
dataType:'json',
data: { value: val }
}).done(function (msg) {
alert("Data Saved: " + msg);
});
}
and you can fetch the result in your 'msg' object.
Note: Do not use getJSON() as it cache responses which may cause your data not in sync with your repository. But you can still use it by settinb cache = FALSE in ajax setting as follows:
$.ajaxSetup({cache:false});
This line should precede your ajax call.
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With