Is there a viewbag variable (or something of the sort) I can set that once I get back into the View, I can make that second button visisble?
Yeah but since I am not a big fan of ViewBag I would suggest you adding a boolean property to your model and then set this property in the POST controller action.
@if (Model.IsSecondButtonVisible)
{
<input type="button" value="the second button" />
}
Seems like when I'm coing back from into the View, looks like I'll need to set some kind of html attribute (based on whether the button should be visibile) that says to make the button visible. How would I accomplish that???
I totally agree... For now, I just want to be able to see via if the hidden attribute of the button can be changed to unhidden based on a value in a viewbag or preferably model.
Once I know it's working with the Viewbag item. I can implement in the Model....
So, the question still remains as to how I can cahnge the hidden attribute of a button to not being hidden based on a value coming back into the view (via a viewbag - for now, or a model).
Yeah but since I am not a big fan of ViewBag I would suggest you adding a boolean property to your model and then set this property in the POST controller action.
@if (Model.IsSecondButtonVisible)
{
<input type="button" value="the second button" />
}
Seems like when I'm coing back from into the View, looks like I'll need to set some kind of html attribute (based on whether the button should be visibile) that says to make the button visible. How would I accomplish that???
Does this not do what you want? If not what about something like the below?
I set up the following, but it still thinks the loanid is set to null when I get back into the View. Can you please verify what I'm doing is correct? I've used dataanootations before, worked fine, and I don't know why this is giving me such issues...
Actually, by putting in the following from my return on my View, it updated the Model for the initial time and was orignally set to fasle which I wanted.
On subsequent hits, the value turned to true once I got data from the database.
wsyeager36
Member
385 Points
458 Posts
Is there a viewbag variable (or something of the sort) I can set that once I get back into the Vi...
Oct 04, 2012 12:50 PM|LINK
Is there a viewbag variable (or something of the sort) I can set that once I get back into the View, I can make that second button visisble?
Yeah but since I am not a big fan of ViewBag I would suggest you adding a boolean property to your model and then set this property in the POST controller action.
[HttpPost] public ActionResult SomeAction(MyViewModel model) { model.IsSecondButtonVisible = true; return View(model);and then inside the view:
@if (Model.IsSecondButtonVisible) { <input type="button" value="the second button" /> }Seems like when I'm coing back from into the View, looks like I'll need to set some kind of html attribute (based on whether the button should be visibile) that says to make the button visible. How would I accomplish that???
Bill Yeager
MCP.Net, BCIP
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Is there a viewbag variable (or something of the sort) I can set that once I get back into th...
Oct 04, 2012 12:57 PM|LINK
This is a question or an answer?!
wsyeager36
Member
385 Points
458 Posts
Re: Is there a viewbag variable (or something of the sort) I can set that once I get back into th...
Oct 04, 2012 01:21 PM|LINK
I totally agree... For now, I just want to be able to see via if the hidden attribute of the button can be changed to unhidden based on a value in a viewbag or preferably model.
Once I know it's working with the Viewbag item. I can implement in the Model....
So, the question still remains as to how I can cahnge the hidden attribute of a button to not being hidden based on a value coming back into the view (via a viewbag - for now, or a model).
Bill Yeager
MCP.Net, BCIP
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Is there a viewbag variable (or something of the sort) I can set that once I get back into th...
Oct 04, 2012 01:27 PM|LINK
ModelState.RemoveKey - before setting value.
wsyeager36
Member
385 Points
458 Posts
Re: Is there a viewbag variable (or something of the sort) I can set that once I get back into th...
Oct 04, 2012 02:48 PM|LINK
Sorry, I don't quite understand....
If I remove the key from model state, when coming back into the View, how is that going to allow me to make visible the btnGeneratePDF button?
Below is the markup of the View:
FConverterModel.ViewModels.ViewModelTemplate_Guarantors @{ ViewBag.Title = "BHG :: PDF Generator"; } <h2>@ViewBag.Message</h2> <div> <table style="width: 1000px"> <tr> <td colspan="5"> <img alt="BHG Logo" src="~/Images/logo.gif" /> </td> </tr> @using (Html.BeginForm("Refresh", "Home", FormMethod.Post)) { <tr> <td> <label for="txtLoanID">Loan ID:</label> @*@(Html.Kendo().NumericTextBox<int>() .Name("txtLoanID") .Placeholder("Enter numeric value") )*@ @Html.Label("lblLoanID", "Loan ID: ") @Html.TextBox("txtLoanID") <td colspan="3"> <input type="submit" id="btnRefresh" value='Refresh' /> </td> </tr> <tr> <td>@Html.Label("lblLoanType1", "Loan Type: ") @Html.Label("lblLoanType2", "SBA") </td> <td> <label for="ddlDept">Department:</label> @(Html.Kendo().DropDownList() .Name("ddlDept") .DataTextField("DepartmentName") .DataValueField("DepartmentID") .Events(e => e.Change("Refresh")) .DataSource(source => { source.Read(read => { read.Action("GetDepartments", "Home"); }); }) ) @(Html.Hidden("ddlDeptText")) </td> </tr> } @if (ViewBag.ShowTemps == true) { using (Html.BeginForm("GeneratePDF", "Home", FormMethod.Post)) { <tr id="trTempLbl" aria-disabled="true" aria-hidden="true"> <td colspan="5"> <b>@Html.Label("lbl_Templates", "Templates:")</b> </td> </tr> <tr id="trTempData" aria-disabled="true" aria-hidden="true"> @foreach (var item in Model.Templates) { <td> @Html.CheckBoxFor(model => Convert.ToBoolean(item.IsChecked)); @Html.LabelFor(model => item.TemplateName) </td> } </tr> <tr id="trGuarLbl" aria-disabled="true" aria-hidden="true"> <td colspan="5"> <b>@Html.Label("lbl_Guarantor", "Guarantor(s):")</b> </td> </tr> <tr id="trGuarData" aria-disabled="true" aria-hidden="true"> @foreach (var item in Model.Guarantors) { <td> @Html.CheckBoxFor(model => Convert.ToBoolean(item.isChecked)); @Html.LabelFor(model => item.GuarantorFirstName + " " + item.GuarantorLastName) </td> } </tr> <tr> <td> <input type="submit" id="btnGeneratePDF" value='Generate PDF' hidden="hidden" /> </td> </tr> <tr> <td colspan="5"> <b>@ViewBag.Error</b> </td> </tr> } } </table> </div> <script type="text/javascript"> $(document).ready(function () { $("#ddlDept").prepend("<option value='All' selected='selected'></option>"); }); var txtLoanID; $("#btnRefresh").click(function () { Refresh(); }); function Refresh() { var txtLoanID = $("#txtLoanID").val(); if (txtLoanID != "") { $("#ddlDeptText").val(this.text()); document.forms["btnRefresh"].submit(); } }Bill Yeager
MCP.Net, BCIP
danielhead
Member
172 Points
36 Posts
Re: Is there a viewbag variable (or something of the sort) I can set that once I get back into th...
Oct 04, 2012 07:01 PM|LINK
Does this not do what you want? If not what about something like the below?
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Is there a viewbag variable (or something of the sort) I can set that once I get back into th...
Oct 05, 2012 01:50 AM|LINK
yes. You do not want what it comes from the view, you want just set the value in the post
ModelState.RemoveKey("IsSecondButtonVisible");
kakashi0124
Member
449 Points
102 Posts
Re: Is there a viewbag variable (or something of the sort) I can set that once I get back into th...
Oct 05, 2012 05:35 AM|LINK
Hi there,
I don't 100% get what you need but seems like you wanna send the data back to the POST setting IsSecondButtonVisible to true.
So when page is initiall loaded, you need something like this on the code
Then what ever you do on the screen, you can set the value of this field using javascript / jquery
$('#IsSecondButtonVisible').val('true') or $('#IsSecondButtonVisible').val('false')Then on the postback you can do this
[HttpPost] public ActionResult SomeAction(MyViewModel model) { //model.IsSecondButtonVisible is set to true or false by the page return View(model); }Then you can use your code to display button or not
@if (Model.IsSecondButtonVisible) { <input type="button" value="the second button" /> }wsyeager36
Member
385 Points
458 Posts
Re: Is there a viewbag variable (or something of the sort) I can set that once I get back into th...
Oct 05, 2012 04:11 PM|LINK
This is more or less what I want...
I set up the following, but it still thinks the loanid is set to null when I get back into the View. Can you please verify what I'm doing is correct? I've used dataanootations before, worked fine, and I don't know why this is giving me such issues...
_Layout.cshtml
<script src="~/Scripts/jquery-1.7.1.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> <script src="~/Scripts/modernizr-2.5.3.js"></script> <script src="@Url.Content("~/Scripts/kendo/2012.2.913/kendo.all.min.js")"></script> <script src="@Url.Content("~/Scripts/kendo/2012.2.913/kendo.aspnetmvc.min.js")"></script>Validation class
namespace PDFService03Validation.Validations { [MetadataType(typeof(PDFService03_Validation))] public partial class PDFService03Validation { } public partial class PDFService03_Validation { public class PDFService03 { [Required(ErrorMessage = "Loan ID Required")] [DataType(DataType.Text)] public string LoanID { get; set; } [Required(ErrorMessage = "Loan Type Required")] [DataType(DataType.Text)] public string LoanType { get; set; } [Required(ErrorMessage = "Department Name Required")] [DataType(DataType.Text)] public string SelectedDeptText { get; set; } } } }ViewModel
public class ViewModelTemplate_Guarantors { public int SelectedTemplateId { get; set; } public IEnumerable<PDFTemplate> Templates { get; set; } public int SelectedGuarantorId { get; set; } public IEnumerable<tGuarantor> Guarantors { get; set; } public string LoanId { get; set; } public string SelectedDeptText { get; set; } public string LoanType { get; set; } public bool ShowTemps { get; set; } public string Error { get; set; } public string ErrorT { get; set; } public string ErrorG { get; set; } public bool ShowGeneratePDFBtn { get; set; } }Controller
public ActionResult Index(ViewModelTemplate_Guarantors model) { ViewBag.Error = ""; model.ShowGeneratePDFBtn = false; return View(); }View
@model PDFConverterModel.ViewModels.ViewModelTemplate_Guarantors @{ ViewBag.Title = "BHG :: PDF Generator"; } <h2>@ViewBag.Message</h2> <div> <table style="width: 1000px"> <tr> <td colspan="5"> <img alt="BHG Logo" src="~/Images/logo.gif" /> </td> </tr> @using (Html.BeginForm("Refresh", "Home", FormMethod.Post, new { id = "frmTemps" })) { <tr> <td> @*@(Html.Kendo().NumericTextBox<int>() .Name("txtLoanID") .Placeholder("Enter numeric value") )*@ @Html.LabelFor(model => model.LoanId) @Html.TextBoxFor(model => model.LoanId) @Html.ValidationMessageFor(model => model.LoanId) <td colspan="3"> <input type="submit" id="btnRefresh" value='Refresh' /> </td> </tr> <tr> <td>@Html.LabelFor(model => model.LoanType) @Html.TextBox("SBA", "SBA") @Html.ValidationMessageFor(model => model.LoanType) @*@Html.TextBoxFor(model => model.LoanType)*@ </td> <td> <label for="ddlDept">Department:</label> @(Html.Kendo().DropDownListFor(model => model.SelectedDeptText) .Name("ddlDept") .DataTextField("DepartmentName") .DataValueField("DepartmentID") .Events(e => e.Change("Refresh")) .DataSource(source => { source.Read(read => { read.Action("GetDepartments", "Home"); }); }) ) @Html.ValidationMessageFor(model => model.SelectedDeptText) </td> </tr> } @if (Model.ShowGeneratePDFBtn == true) { using (Html.BeginForm("GeneratePDF", "Home", FormMethod.Post)) { if (Model.ErrorT != string.Empty) { <tr> <td colspan="5"> <u><b>@Html.Label("Templates:")</b></u> </td> </tr> <tr> @foreach (var item in Model.Templates) { <td> @Html.CheckBoxFor(model => Convert.ToBoolean(item.IsChecked)); @Html.LabelFor(model => item.TemplateName) </td> } </tr> } else { Model.Error = Model.ErrorT; } if (Model.ErrorG != string.Empty) { <tr> <td colspan="5"> <u><b>@Html.Label("Guarantors:")</b></u> </td> </tr> <tr> @foreach (var item in Model.Guarantors) { <td> @Html.CheckBoxFor(model => Convert.ToBoolean(item.isChecked)); @Html.LabelFor(model => item.GuarantorFirstName + " " + item.GuarantorLastName) </td> } </tr> } else { Model.Error = Model.ErrorG; } <tr> <td> <input type="submit" id="btnGeneratePDF" value='Generate PDF' hidden="hidden" /> </td> </tr> <tr> <td colspan="5"> @Model.Error </td> </tr> } } </table> </div> <script type="text/javascript"> $(document).ready(function () { $("#ddlDept").prepend("<option value='All' selected='selected'></option>"); }); var txtLoanID; $('btnRefresh').on('click', '#btnRefresh', function () { Refresh(); }); function Refresh() { var LoanID = $("#LoanID").val(); if (LoanID != "") { document.forms["frmTemps"].submit(); } } </script>RAZOR validation asp.netmvc
Bill Yeager
MCP.Net, BCIP
wsyeager36
Member
385 Points
458 Posts
Re: Is there a viewbag variable (or something of the sort) I can set that once I get back into th...
Oct 05, 2012 08:22 PM|LINK
Actually, by putting in the following from my return on my View, it updated the Model for the initial time and was orignally set to fasle which I wanted.
On subsequent hits, the value turned to true once I got data from the database.
public ActionResult Index(ViewModelTemplate_Guarantors model) { ViewBag.Error = ""; model.ShowGeneratePDFBtn = false; return View("Index", model); }RAZOR validation asp.netmvc
Bill Yeager
MCP.Net, BCIP