I have 2 different lists: EmployeeNames and Names
I read the values in Names and that of EmployeeNames.
If EmployeeNames exists in Names, I must not add that value to "ToSelectBox" but to "FromSelectBox".
If EmployeeNames doesn`t exist in Names, I must add that value to "ToSelectBox" but not to "FromSelectBox".
How can I do that dynamically?
I have 2 option values as follows:
<select id="fromSelectBox" multiple="multiple" >
<% foreach (var item in Model.EmployeeNames) { %>
<option value="<%=Html.Encode(Item.Text)%>"><%=Html.Encode(item.Text)%></option>
<%}
%>
</select>
select id="ToSelectBox" multiple="multiple" >
<% foreach (var item in Model.Names) { %>
<option value="<%=Html.Encode(Item.Text)%>"><%=Html.Encode(item.Text)%></option>
<%}
%>
</select>
You have to examine the data, and programmably control that. You can do that in the controller, where you can use Linq to check for the existence of a name in one list, and follow your business rules there, then pass the final lists to a model to pass to the view.
You could put that logic in your view, but that defeats the purpose of the controller and the separation of layers.
Related
I'm trying to using thymeleaf as the view template and i come across one problem. the code I have now is as below.
<select>
<option th:each="res: ${result1}"
th:value="${result1.NAME}"
th:text="${result1.NAME}"></option>
</select>
What I need is like this, where I dont know how to add another attribute for result2, both result1 and result2 are the list object in the controller
<select>
<option th:each="res: ${result1}"
th:value="${res.NAME}"
th:text="${res.NAME+result2.SCHOOL}"></option>
</select>
You can create a simple object with two fields for result1 and result2, if you know their relationship in the controller. Then you can use a list of that objects to iterate in html page.
<select>
<option th:each="item: ${itemList}"
th:value="${item.result1.NAME}"
th:text="${item.result1.NAME + item.result2.SCHOOL}"></option>
</select>
i want to get the value of a checkbox... my Checkbox show the Value of the Database "true" or "false" and the user can change the Value (if he dont like it)
i get every time the Value false
View:
<%if ((Boolean)ViewData["Statistik3"])
{%>
<input type="checkbox" name="Statistik3" value="true" checked= "checked"/>
<%}
else
{ %>
<input type="checkbox" name="Statistik3" value="false"/> <%--</input>--%>
<%} %>
Controller C#
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Formular(string button, string decision1, FormEntries entries )
{
entries.Statistik3 ? "Yes" : "No"
}
Try rendering the checkbox using a server side Html helper. Don't hardcode input elements as you did in your views because this leads to horrible spaghetti code. So replace the entire code you have shown with a simple call to the Html.CheckBox helper which will take care of rendering the proper input element:
#Html.CheckBox("Statistik3")
and now the controller action you are posting to could take a boolean argument with the same name:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Formular(bool statistik3, string button, string decision1, FormEntries entries)
{
// you could use the statistik3 boolean argument to determine
// whether the user checked or not the checkbox
}
The next improvement I would do would be to completely get rid of ViewData and define a view model that will contain all the information this view requires and then use the strongly typed version of the helper: Html.CheckBoxFox.
Checkboxes are boolean only types; the value attribute will be ignored and it's only where there is the presence of the absence of the checked attribute. Is you form bound to a data model at all? Try the following:
<%if ((Boolean)ViewData["Statistik3"])
{%>
<input type="checkbox" name="Statistik3" id="Statistik3" checked="checked"/>
<%}
else
{ %>
<input type="checkbox" name="Statistik3" id="Statistik3"/> <%--</input>--%>
<%} %>
and then test for a boolean value on return.
Hope that helps.
you need to make sure the Checkbox is wrapped within a form element. Something like this
<% using(Html.BeginForm("Formular")){
<%if ((Boolean)ViewData["Statistik3"])
{%>
<input type="checkbox" name="Statistik3" value="true" checked= "checked"/>
<%}
else
{ %>
<input type="checkbox" name="Statistik3" value="false"/> <%--</input>--%>
<%} %>
<%} %>
As suggested above, you should be using #Html.CheckBox("Statistik3")
I have the following problem:
I have the variable $GiftID on my page.
I want to cycle through all of my gift objects using my function getGifts().
When the $ID of the gift is equal to the $GiftID of the page then I want something to happen.
Here is an example of my code:
$GiftID
<% control getGifts %>
<% if CurrentPage.GiftID = ID %>This is it!<% end_if %>
<% end_control %>
Using $CurrentPage.GiftID works when printing inside the control, but how on earth do I access it from within the if statement?
I am using SS 2.9
I have not used ss2.9 yet, but as far as I know you can not do <% if Top.GiftID = ID %> in any 2.x version, you can not compare 2 variables, you can only compare with static vaules. (but it is possible in 3.0)
So you have to do it on php side, if you want to only display the slected gift object, then:
if GiftID is actually the DB field for the has_one relation of Gift then you can just do <% control Gift %> and it will scope the Gift object with the GiftID
If you really have GiftID saved as DB field or otherwise, then can do
public function getGift() { return DataObject::get_by_id('Gift', $this->GiftID); }
both ways you can do <% control Gift %> and it will scope it
If you want to list all gifts and mark the current gift then you need to do it on php side (foreach the set of objects and set a flag on the current object)
You should be able to access the current page with Top:
<% control getGifts %>
<% if Top.GiftID = ID %>This is it!<% end_if %>
<% end_control %>
I have a form that has a drop-down list of values and a submit button.
Currently, when you click on the submit button, a stored procedure is called and then the application generates a url and then the ActionResult is a Redirect to a new window. The url is based on the currently selected value in the dropdown list.
Our client wants another button that when clicked, will basically do the same thing, but FOR ALL VALUES in the drop down list.
Basically, on click, multiple windows will be opened, whose urls each based on a value in the drop down list.
I just started working with MVC and research confused me even more. I'm hoping someone can point me in the right direction.
Should I handle this via some sort of loop in javascript? How? Can you give some examples, please?
ASPX Portion:
<div id="MyContainer" class="select-report">
<%
using (Html.BeginForm(MyManager.Query.Actions.GenerateReport(null), FormMethod.Post, new{target="_blank"}))
{%>
<select name="SearchText" class="my-values-select">
<% foreach (var cc in Model.MyCentresList)
{%>
<option value="<%=Html.Encode(cc.Name) %>">
<%=Html.Encode(cc.Name) %></option>
<% } %>
</select>
<input type="hidden" name="SearchType" value="MyCentre" />
<input type="submit" value="Generate" name="EntityName" />
<% } %>
</div>
Code-Behind:
public virtual ActionResult GenerateReport(GenerateReportOperation operation)
{
string entityName = operation.SearchText;
int entityType = (int)operation.SearchType;
string requestID1 = <code here that calls a stored procedure, a value is returned>;
string requestID2 = <code here that calls a stored procedure, a value is returned>;
string urlString = <code here that contructs the URL based on the values of entityName, entityType, requestID1, requestID2>;
return Redirect(urlString);
}
You would have to use JavaScript to open new windows for each individual HTTP request.
I am building a .NET MVC app that has a page with a list of delete buttons, one for each item in a list. The problem I'm having is that the foreach variable "item" is not visible inside the LoginView, which results in the following error:
Compiler Error Message: CS0103: The name 'item' does not exist in the current context
Below is a simplified version of the view. The error occurs at the "new {id=item.Id}" in the LoggedInTemplate - the reference to "item" in the ActionLink works fine:
<% foreach (var item in Model) { %>
<%= Html.ActionLink("Item", "Details", new { id = item.Id })%>
<asp:LoginView runat="server">
<LoggedInTemplate>
<% using( Html.BeginForm( "Delete", "Items", new {id=item.Id}, FormMethod.Post))
{ %>
<input type="submit" value="Delete" runat="server" />
<% } %>
</LoggedInTemplate>
</asp:LoginView>
<% } %>
To clarify the problem is not that the Model has not been successfully passed to the View. The Model is visible from both inside and outside the LoginView. The foreach loop as no problem in iterating through the items in the Model (which is a List). The problem is that the iteration variable "item" is not accessible from within the LoginView - though the original Model is.
Is there any way to pass "item" through to the LoginView's templates? Or is building LoginViews within a foreach loops the wrong way of doing things?
Is there a scoping rule that prevents using local variables within controls - perhaps because the control is rendered at a different time to the main page?
With ASP.NET MVC you really shouldn't use user/custom controls, so if you omit the <asp:LoginView/> and write a line of code to check if the user is authenticated, you are good to go.
Instead of your current code:
<asp:LoginView runat="server">
<LoggedInTemplate>
<div>Show this to authenticated users only</div>
</LoggedInTemplate>
</asp:LoginView>
Just use an if-statement and the value of Request.IsAuthenticated:
<% if (Request.IsAuthenticated) { %>
<div>Show this to authenticated users only</div>
<% } %>
Are you passing the Model to the view and are you also inheriting from the model within that view?
So if this is a View then in your C# code you need to return the list of items like return View(listofitems);
If this is a partial view then <% Html.RenderPartial("MyPartial", listofitems) %>
And in the view you need to
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IQueryable<ListOfItems>>" %>
If all that is in place then it should work no probs.
<% foreach (var item in Model) { %>
<%= Html.ActionLink("Item", "Details", new { id = item.Id })%>
<%= if( Request.IsAuthenticated ) {
using( Html.BeginForm( "Delete", "Items", new {id=item.Id}, FormMethod.Post))
{ %>
<input type="submit" value="Delete" runat="server" />
}
} %>
<% } %>
There is no need to use the LoginView, its not really giving you anything. Use something like the above instead.
Alternatively, you can move the decision of whether to show the delete option for the specific item into the controller, so instead of doing if( Request.IsAuthenticated ) you would do if( item.ShowDelete ) ... assuming item's type is a view model. Another option is to use an extension method for the same, item.ShowDelete(). I prefer the earlier, because there might be logic associated to deciding whether to show delete for a given item, so its better to not have it in the controller or a related logic.