MVC Razor String Concat - asp.net-mvc

I am using Razor to generate a form. I want to create HTML elements based on some value from it's model property.
for example, if a model contains Id property, and I want to generate html tags as follows
<input type="hidden" name="1_chk" />
<input type="hidden" name="2_chk" />
<input type="hidden" name="3_chk" />
So I used the following syntax, and it failed. Can anyone help me out with this?
<input type="checkbox" name="#Id_chk" />
Thanks

I think this should work for you:
<input type="checkbox" name="#(Id)_chk" />

another option:
<input type="checkbox" name="#(Id + "_chk")" />

Related

Prevent user to type in a textbox [duplicate]

what is the code to disable an INPUT text box for HTML?
Thanks
<input type="text" disabled="disabled" />
See the W3C HTML Specification on the input tag for more information.
<input type="text" required="true" value="" readonly="true">
This will make a text box in readonly mode, might be helpful in generating passwords and datepickers.
The syntax to disable an HTML input is as follows:
<input type="text" id="input_id" DISABLED />
You can Use both disabled or readonly attribute of input . Using disable attribute will omit that value at form submit, so if you want that values at submit event make them readonly instead of disable.
<input type="text" readonly>
or
<input type="text" disabled>
<input type="text" required="true" value="" readonly>
Not the.
<input type="text" required="true" value="" readonly="true">

Get posted values from dynamically added controls in ASP.NET MVC

I have a form in which i'm dynamically addding controls through jQuery. I need to access the values in those controls (textboxes) when posting back the form to the server. I'm sure this is a trivial problem but i just can't get my head around it.
Any help would be greatly apreciated.
When adding a multiple controls to the page, give them all the same name attribute so you can do the following in your action:
public ActionResult MyAction(string[] items)
{
// items will contain all the values in the text boxes
return View();
}
So your HTML would like like this
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />

Lists as mvc controller method arguments?

I have 2 tables containing checkboxes in a MVC form. The names of the checkboxes are currently more or less random.
Can I name the items in any smart way so that I can retrieve them as two named lists as controller method arguments? Preferably if I could do with prefixing the names.
<div>
<input type="checkbox" name="xyz" />
<input type="checkbox" name="foo" />
<input type="checkbox" name="123" />
</div>
<div>
<input type="checkbox" name="bar" />
<input type="checkbox" name="456" />
<input type="checkbox" name="baz" />
</div>
Can I somehow get it as arguments, similar to this?
public ActionResult DoThis(BlablahViewModel model, string[] firstList, string[] secondList)
{
Currently I just check for their existence roughly like this:
Request.Form["xyz"].Contains("t")
Thanks!
You'll need to use the List Model Binding features of MVC:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
For a good guide of how this can all work together:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

How to gather arbitrary length list data in ASP.NET MVC

I need to gather a list of items associated with another item from my user in a ASP.NET MVC project. I would like to have a controller action like bellow.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int x, int y, IEnumerable<int> zKeys)
{
//Do stuff here
}
How can I setup my form to pass data in this way? If data of this particular form can't be provided, what's the next best way to pass this type of information in ASP.NET MVC?
Scott Hanselman has an excellent article on how to do this here:
ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries
http://www.hanselman.com/blog/...BindingToArraysListsCollectionsDictionaries.aspx
<form action="/url" method="post">
<input name="x" type="text" value="1" />
<input name="y" type="text" value="1" />
<div>
<input name="zKeys" value="1" />
<input name="zKeys" value="2" />
<input name="zKeys" value="3" />
<input name="zKeys" value="4" />
<input name="zKeys" value="5" />
<input name="zKeys" value="6" />
<input name="zKeys" value="7" />
</div>

ASP.NET MVC Partial View with Form

I have a scenario I want to use a partial view but I'm having issues with it passing data to the controller. Here is a basic example of what I'm trying to do.
Objects:
Customer
Order
A Customer has an IList<Order> on it. I want the partial view to allow the user to edit the information. I can get the data to display but when the form posts the list under the Customer object is null.
I have also attempted to use a seperate form in my partial view. When I do this if I create paramenters on the controller like so I get the data:
public ActionResult UpdateOrders(IList<Guid> id, IList<int> quantity, IList<Guid> productId)
But when I do this
public ActionResult UpdateOrders(IList<Order> orders)
The list is null.
If anyone has a better suggestion of how to achieve this let me know.
How are you referencing the fields in your view? I'm thinking that it should be something like:
<input type="hidden" name="orders.Index" value="0" />
<input type="hidden" name="oders[0].ID" value="1" />
<input type="hidden" name="orders[0].productId" value="4" />
<input type="text" name="orders[0].quantity" value="6" />
<input type="hidden" name="orders.Index" value="1" />
<input type="hidden" name="orders[1].ID" value="2" />
<input type="hidden" name="orders[1].productId" value="2" />
<input type="text" name="orders[1].quantity" value="15" />
See Phil Haack's blog entry on binding to a list for more info.

Resources