String concatenation with ASP.NET MVC3 Razor - asp.net-mvc-3

i'm trying to concatenate a string in asp.net mvc 3 razor and i'm getting a little sintax problem with my cshtml.
i what to generate an id for my checkboxes on a foreach statement, and my checkboxes should start with "chk" and what to cancatenate a fieldon the ID, something like that:
<input type="checkbox" id="chk+#obj.field" />
but or exampple the result for id attribute is: id="chk+8"
how can i just get a result for something like "chk8"?

Just put your variable next to prefix:
<input type="checkbox" id="chk#(obj.field)" />

Try
<input type="checkbox" id="#("chk" + obj.field)" />
or
<input type="checkbox" id="chk#obj.field" />

<input type="checkbox" id="chk#(obj.field)" /> should work.
The most direct and clean way to add a prefix a suffix.
#("PREFIX " + obj.field + " SUFFIX")

<input type="checkbox" id="chk#(obj.field)" /> should work.

Best way to concate any C# variable in rozer view by using string.Format
id="#string.Format("{0}_Title", _Id)" // Apend after
id="#string.Format("Title_{0}", _Id)" // Apend before
id="#string.Format("Title_{0}_Title", _Id)" // Apend Middle

Related

How to set default value in thymeleaf th:field

I have a form and I want to set default value in the field below but it's not working.
<span>ID User:</span>
<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />
</div>
<div>
<span class="name-in">ID Room:</span>
<input type="text" th:value="${id}" th:field="*{room}" th:errorclass="field-error" />
</div>
I read some topic about this problem and I try to change as given below
th:attr="value = ${session.name}"
But It's still not working. Field ID User is empty. I don't know how to solve this problem.
Although your question contain less information, but i think you want to put default value for all field. If you like to do so change
`<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />`
to
<input type="text" name="userid" value="as your wish" th:errorclass="field-error" />
Instead of changing the html, you should instead set the value of *{userid} in your controller. That way you can keep your html the same:
// Controller
modelObject.setUserId(session.name);
// HTML
<input type="text" th:field="*{userid}" th:errorclass="field-error" />

Set maximum length for field in joomla

I want to set max length from default.php.
Here is my XML format,
<fieldset name="sender_details">
<field name="sendername"
type="text"
label="SENDERV_SENDER"
description="SENDERV_SENDER_DESC"
default=""
required="true" />
</fieldset>
Here is the coding for default.php.
foreach ($this->form->getFieldset('sender_details') as $field) :
echo $field->label;
echo $field->input;
}
I had a variable $length=5.The text box should allow only 5 characters.The length is coming from the configuration. I need to do in default.php
kindly Help Me.
To specify max length in Joomla Text Field Like This
<fieldset name="sender_details">
<field name="sendername"
type="text"
label="SENDERV_SENDER"
description="SENDERV_SENDER_DESC"
default=""
maxlength="5"
required="true" />
</fieldset>
if you want your custom attribute then you can create joomla new field type or you can modify joomla form fields
libraries/joomla/form/fields/text.php
libraries/joomla/form/fields/textarea.php
libraries/joomla/form/fields/.......
any problem implementing please reply
The problem with editing core-files (libraries/joomla/form/fields/text.php, libraries/joomla/form/fields/textarea.php, libraries/joomla/form/fields/...) is, that with the next joomla-update these changes could be overwriten again.
Until joomla does noch correct this bug, is there any possibility to set a custom html-attribute to a jformfield before it renders with "print $field->input;" ?
edit: There already exists a bug-entry for that: http://issues.joomla.org/tracker/joomla-cms/3510?lang=de-DE
You can set in edit layout like this :
<?php echo $this->form->getInput('fieldname','maxLength',4); ?>
And then your html code is :
<input type="text" name="jform[fieldname]" id="jform_fieldname" maxlength="4">

Codeigniter auto change the input field value

when i give an input field value as blackhat%%1985 and submit i get the post value as blackhat%85
the value is changed after require_once BASEPATH.'core/CodeIgniter.php';
<form action="" method="post" id="submitForm">
<input type="hidden" name="a" value="blackhat%%1985" />
<input type="submit" name="b" />
</form>
use urlencode() function for this.
You can use JavaScript's encodeURIComponent to encode the value before submitting the form.
encodeURIComponent('blackhat%%1985');
Of course, don't forget to decode it on the server-side after that.

MVC3 : data retrived from ViewData contains "/"

I have a small problem. I'm passing a id from one view to another.
I do it like this:
#Html.Partial("DetailViews/_Parameters", Model.Parameters, new ViewDataDictionary { { "data-resourceId", Model.Id } })
I use this id for a hidden field in a form on the partial view. like this:
<form id="formAddNewParameter" action"#" title="Add new Parameter">
<input type="hidden" name="resourceId" id="resourceId" class="required" rel="0" value=#ViewData["data-resourceId"]/>
</form>
The ID is passed as it should. BUT there is always a "/" at the end...
Anyone know where this "/" comes from.
You are missing the qutation marks "" around the value tag's value
<form id="formAddNewParameter" action"#" title="Add new Parameter">
<input type="hidden" name="resourceId"
id="resourceId" class="required" rel="0"
value="#(ViewData["data-resourceId"])" />
</form>
And I guess the tag closing / somehow mixed into the value tag.

Form fields added via AJAX fail to load into the $_POST array

I've got a plain and simple HTML form which allows people to order some brochures. The form first loads with something looking a little like this:
<script type="text/javascript">
var tableRowN = 1;
</script>
<form id="Order" name="Order" method="post" action="includes/orderCheck.php">
<input id="name" type="text" name="name" width="100" />
<table id="orderingTable">
<tr class="lastRow">
<td><div id="itemGroupdiv1">
<input type="text" class="disabled" name="itemGroup1" id="itemGroup1" />
</div></td>
<td><div id="itemCodediv1">
<input type="text" name="itemCode1" id="itemCode1" class="disabled" />
</div></td>
<td><div id="itemCodeVersiondiv1">
<input type="text" class="disabledSmall" id="itemcodeversion1" name="itemcodeversion1" />
</div></td>
</tr>
</table>
<input type="submit" name="submit" id="submit"/>
</form>
Then when the user wants to add a new line to the table he can click a button which fires the following javascript function to grab the new table code via AJAX and insert it.
function createItemLine() {
tableRowN++;
$('tr.lastRow').attr('class', '');
$('#orderingTable').append('<tr class="lastRow"></tr>');
$.ajax({
url: "/orderingTable.php?rNumber=" + tableRowN,
cache: false,
success: function(html){
$("tr.lastRow").append(html);
alert('loaded');
}
});
}
The AJAX function then runs off to a PHP script which creates the next line, rolling the IDs and Names etc with +1 to the number.
<td><div id="itemGroupdiv2">
<input type="text" class="disabled" name="itemGroup2" id="itemGroup2" />
</div></td>
<td><div id="itemCodediv2">
<input type="text" name="itemCode2" id="itemCode2" class="disabled" />
</div></td>
<td><div id="itemCodeVersiondiv2">
<input type="text" class="disabledSmall" id="itemcodeversion2" name="itemcodeversion2" />
</div></td>
So so far, nothing suprising? Should all be pretty straight forward...
The problem is that when I add new lines (In Firefox and Chrome) the new lines are completely ignored by the form submission process, and they never get passed through into the $_POST array.
Is this a known problem? I've not come across this before...
Thanks for any pointers,
H
use jQuery.trim(data) but this is not pretty sure because can affect the
content of your data. or see this one may help u
Is your table missing an html id? The jQuery selector $('#orderingTable') is looking for something with id="orderingTable"
On some thorough (and boy do I mean thorough) it turned out that the following simple (yet obvious) HTML errors can cause this issue:
Badly formed code EG missing etc
Duplicate or missing form "name" attributes
On creating properly validated HTML, the form submitted and all values were passed correctly into the _POST array. An object lesson in making sure your developers pay attention to the basics before trying to get all fancy in their coding approach ;)
I've found that using .html() to insert the content instead of .append() or .prepend() causes the inserted form fields to work as expected.
I've just spent quite a while laboring over a problem like this.
I was ajax-ing an input field into a form and that input field was not showing up in the $_POST submission array, was completely annoying!!!! Aaaaanyway, I fixed it by just checking over all my html and it turns out that my form 'open' was inside one of the main div's on page and not outside.
thus:
<div>
<form>
<input type="text" name="input_field">
</div>
</form>
is now fixed to be:
<form>
<div>
<input type="text" name="input_field">
</div>
</form>
Silly, I know, but in a massive form, it was tricky to spot! So in short just be tidy with your html and it WILL work, I hope that helps someone somewhere :-)
M

Resources