if statement should check part of string exists in mvc - asp.net-mvc

Below code saying substring doesnt exist in current context. Give me someother way to check whether my id contains 'DOM' or 'INT' in my view in mvc.
My table:
My code:
#if( (SUBSTRING(Model.Id,7,9)) !== 'DOM' )
{
<tr>
<td>Amount Approved for Domestic sector : </td>
<td><input id="Text1" type="text" /></td>
</tr>
}

You could use the Contains method to check if a given string contains a specified substring:
#if(Model.Id.Contains("DOM"))
{
<tr>
<td>Amount Approved for Domestic sector : </td>
<td><input id="Text1" type="text" /></td>
</tr>
}
or:
#if(Model.Id.Split('/')[1].Trim() == "DOM")
{
<tr>
<td>Amount Approved for Domestic sector : </td>
<td><input id="Text1" type="text" /></td>
</tr>
}

I think it should be
#if((Model.Id.Substring(7,9)) !== 'DOM')
Since Substring is a method of String

Related

Bind ASP.NET MVC model value to checkbox with knockout.js

I am new to knockout.js. I have a Y/N value from my model that I want to bind to a checkbox.
This is my view:
<tbody id="tblMultiEdit" data-bind="foreach: UUTs">
<tr>
<td data-bind="text: SerialNumber"></td>
<td><input type="checkbox" data-bind="ReqDowngrade" /></td>
<td><input type="checkbox" data-bind="ACTSupported"/></td>
<td><input type="checkbox" data-bind="ProdModeOff"/></td>
</tr>
</tbody>
Knockout code:
function ViewModel(UUTs)
{
var self = this;
self.UUTs = UUTs;
};
var viewModel = new ViewModel(#Html.HtmlConvertToJson(Model));
ko.applyBindings(viewModel);
So far I can bind the text with no problem, but checkboxes are in blank.
I was able to resolve this by adding a ternary operation.
<td><input type="checkbox" data-bind="checked: (ReqDowngrade == 'Y' ? 1 : 0)" /></td>
If anybody knows a more efficient way using knockout let me know!

Multi HTML elements same ID in MVC

I am generating multi Data entry Line using ASP MVC , however
looking at the HTML source after generating by MVC, I've just noticed those HTML elements has a duplicated ID , even with the same html type it is look something wrong ? is that normal behavior in MVC and how to avoid it ? , I need really to have it unique, assuming MVC should handle the ID generation for multi line, which is additional concern to me .
My code :
#foreach (var item in Model.lstMeals)
{
<tr>
<td>
<input asp-for="#item.cuisine.CuisineName" />
#Html.DisplayFor(modelItem => item.cuisine.CuisineName)
</td>
</tr>
}
then looking in HTML Source :
<tr>
<td>
<input type="text" id="item_cuisine_CuisineName" name="item.cuisine.CuisineName" value="Italian" />
Italian
</td>
</tr>
<tr>
<td>
<input type="text" id="item_cuisine_CuisineName" name="item.cuisine.CuisineName" value="French" />
French
</td>
</tr>
<tr>
<td>
<input type="text" id="item_cuisine_CuisineName" name="item.cuisine.CuisineName" value="Greek" />
Greek
</td>
</tr>
If you use tag-helper within a loop, it won't generate an unique id for you for each element It doesn't have a context of the index position within the loop.
With that said, you can easily generate your own unique id "algorithm" for your HTML elements inside the loop with usage of the index:
#for(int i = 0; i < Model.lstMeals.Count(); i++)
{
<tr>
<td>
<input asp-for="#Model.lstMeals[i].cuisine.CuisineName"
type="text"
id="xxx-xxx-lstmeals-#i" />
</td>
</tr>
}
I think you can do it with for loop something like this.
#for (int i = 0; i < Model.lstMeals.Count; i++)
{
<tr>
<td>
<input asp-for="#Model.lstMeals[i].item.cuisine.CuisineName[i]" />
#Html.DisplayFor(modelItem => item.cuisine.CuisineName)
</td>
</tr>
}

MVC How to pass label data from view to controller?

I am trying to pass data from view to controller. I used BeginForm and i can pass data which users enter to textbox. But I want to also pass label data because label is filled automatically and i need to save this label text to database. How can i do?
View:
#using (Html.BeginForm("Room", "Booking", FormMethod.Post))
{
<table>
<tr>
<td align="left"><lable for="eventName">Description:</lable></td>
<td><input name="eventName" id="eventName"></td>
</tr>
<tr>
<td align="left"><lable for="startDate">Start Date : </td>
<td align="left"><label id="startDate" name="startDate" /></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td align="right" colspan="2">
<button type="submit" class="btn-primary" name="submit" id="submit">Save</button>
</td>
</tr>
<tr>
</tr>
</table>
}
Controller:
[HttpPost]
public ActionResult Room(FormCollection form)
{
using (BookingEntities ent = new BookingEntities ())
{
ReservationTBL Tbl = new ReservationTBL();
Tbl.Description = form["eventName"].ToString();
Tbl.startDate= form["startDate"].ToString();
ent.BookingTBL.Add(Tbl);
ent.SaveChanges();
}
return View();
}
The label data doesn't get send when you post your form. What you can do however is to add an input with type hidden that contains your label data.
Something like this:
<tr>
<td align="left"><label for="startDate">Start Date : </td>
<td align="left">
<label>#Model.StartDate</label>
<input type="hidden" name="startDate" value="#Model.StartDate" id="startDate"/>
</td>
</tr>

How to retrieve struts2 checkboxlist values

I will create a list with checkboxlist. For those I use following code:
<s:form action="accept" namespace="/manager/course">
<s:checkboxlist list="courseRequests" name="acceptList" listValue="studentNickname" listKey="studentId" theme="checkbox-fix"/>
<s:url action="accept" namespace="/manager/course" var="accList" />
<s:a href="%{accList}"><s:text name="Accept"/> </s:a>
</s:form>
It work fine a create a check box list, that you can see its pic in the following:
and this is html code generated by above code:
<form id="accept" name="accept" action="/ESA/manager/course/accept.action" method="post">
<table class="wwFormTable">
<table class="gradienttable">
<tr>
<th class="row"><p>Row</p></th>
<th style="width: 240px;"><p>Student</p></th>
<th ><p>Accept</p></th>
</tr>
<tr>
<td id="row"><p><label>1</label></p></td>
<td style="width:250px;"><p>
<label for="acceptList-1" class="checkboxLabel">Mansour Barzegar</label>
</p></td>
<td style="text-align:center;"><p>
<input type="checkbox" name="acceptList" value="5" id="acceptList-1" </p></td>
</tr>
<tr>
<td id="row"><p><label>2</label></p></td>
<td style="width:250px;"><p>
<label for="acceptList-2" class="checkboxLabel">Ali Mahmoudi</label>
</p></td>
<td style="text-align:center;"><p>
<input type="checkbox" name="acceptList" value="6" id="acceptList-2" </p></td>
</tr>
<tr>
<td id="row"><p><label>3</label></p></td>
<td style="width:250px;"><p>
<label for="acceptList-3" class="checkboxLabel">Masih Zare</label>
</p></td>
<td style="text-align:center;"><p>
<input type="checkbox" name="acceptList" value="7" id="acceptList-3" </p></td>
</tr>
</table>
Accept
</table>
</form>
In the Action Class I tried to retrive seleced checkbox value by following code:
private int[] acceptList;
public void setAcceptList(int[] acceptList){
this.acceptList=acceptList;
}
and several other code but I all states I get null.
Do I use wrong code?
in your markup, do this:
<input type="checkbox" name="thename" id="checkbox_id1" value="value1" />
<input type="checkbox" name="thename" id="checkbox_id2" value="value2" />
in your action (or object) do this:
// action/object code...
Set<String> cbox = new HashSet();
public void setThename(String[] thenames) {
for (String thename : thenames) {
cbox.add(thename);
}
}
// action/object code...
notice the checkbox name matches the setter name, e.g. element name == someName and method == setSomeName
Same would apply for Set<Integer>, but you use int[] thenames as the argument. You could also use Integer[] thenames for the argument.
to test output:
if (cbox != null) {
for (String s : cbox) {
log.info(s);
}
}
http://struts.apache.org/release/2.2.x/docs/using-checkboxes.html

two-way binding of a single object in observableArray

My page is as follows:
<button id="add">Add Data</button>
<button id="show">show</button>
<table>
<tr style="vertical-align:top">
<td>
<table border="1">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody data-bind="foreach: students">
<tr>
<td data-bind="text: id"></td>
<td>
<input type="text" data-bind="value: name" />
</td>
<td> Select
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table id="data">
<tbody data-bind="with: selectedData">
<tr>
<td>Id</td>
<td>
<input type="text" data-bind="value: id" />
</td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" data-bind="value: name" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="button" value="Close" />
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
The javascript is as follows:
function ViewModel() {
var self = this;
self.students = ko.observableArray([]);
self.showData = function (dt) {
if (window.console) console.log(dt);
self.selectedData(dt);
$('#data').show();
}
this.selectedData = ko.observable();
}
$(function () {
window.appViewModel = new ViewModel();
ko.applyBindings(window.appViewModel);
$('#add').click(function () {
var model = window.appViewModel;
$.each(students, function (idx, student) {
if (window.console) console.log(student);
model.students.push(student);
});
$('table').show();
});
$('table').hide();
$('input').click(function () {
$('#data').hide();
});
$('#show').click(function () {
var s = JSON.stringify(window.appViewModel.students());
alert(s);
});
});
Preview:
In pic, I click on the select corresponding to student with id = 3. The other table shows up with the selected student details. Suppose I enter something in textbox 1, textbox 2 doesn't update, and vice versa.
What to do to make that happen?
Fiddle: http://jsfiddle.net/deostroll/YdrQf/1/
Your inputs aren't updating because the id and name values are not being stored or bound against observables, which are the special object that knockout provides specifically for this purpose. You can easily solve this with your code by adding a new Student type:
function Student(data) {
this.id = ko.observable(data.id);
this.name = ko.observable(data.name);
};
and use it to populate your students array with:
$.each(students, function (idx, student) {
if (window.console) console.log(student);
model.students.push(new Student(student));
});
With those properties now being observables, their changes will propagate to the UI. Here is the fiddle, with these two minor changes.
That being said, I think you have largely missed the point of Knockout. I strongly suggest you go through the Knockout tutorials, if you haven't done so already.
You're use of jQuery to create click functions for your viewmodel really goes against the model that Knockout encourages. Please take a look at this fiddle, which converts your code into 100% Knockout, using viewmodel functions, and drops all the jQuery.

Resources