ASP.NET MVC complex model retrieving on the server [duplicate] - asp.net-mvc

This question already has answers here:
Collection of complex child objects in Asp.Net MVC 3 application?
(2 answers)
Closed 8 years ago.
Let's say I have model with property:
public List<string> Subscribers { get; set; }
I am rendering markup like that to receive such list on the server on form submit:
<input type="hidden" name="Subscribers[0]" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[1]" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[2]" value="SomeSubscriber" />
What if subscriber is a type with Name and ID properties. How should markup look like to get the whole list of subscribers back on form submit?
The challenge is to render markup aproppriately and without use of any editorFor templates

That would be :
<input type="hidden" name="Subscribers[0].Name" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[0].ID" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[1].Name" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[1].ID" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[2].Name" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[2].ID" value="SomeSubscriber" />

Related

How to send http post form parameters to worldpay using mvc 3(Integration With Worlpay using MVC)

Can any one help me in passing the parameters to the worldpay site using mvc http post , below is the example which i found on google , the example is working on view , but I want to pass the parameters through http[post] action controller :
<form method="post" action="https://secure.wp3.rbsworldpay.com/wcc/purchase" id="frmWorldPay">
<input type="hidden" name="instId" value="1" />
<input type="hidden" name="cartId" value="<%: Model.CardID %>" />
<input type="hidden" name="currency" value="GBP" />
<input type="hidden" name="amount" value="<%= Model.Cost%> " />
<input type="hidden" name="desc" value="<%: ViewBag.Name %> track day" />
<input type="hidden" name="email" value="<%: Model.aspnet_Users.aspnet_Membership.Email %>" />
<input type="hidden" name="name" value="<%: Model.FullName %>" />
<input type="hidden" name="address" value="<%: Model.Address %>" />
<input type="hidden" name="testMode" value="100" />
Have a look at System.Net.WebClient.
Also this question on SO may help you further.
edit
You should follow the links I posted. There you find i.e. this example code
using System;
using System.Text;
using System.Net;
using System.Collections.Specialized;
//...
string url = "http://www.amazon.co.uk/exec/obidos/search-handle-form";
NameValueCollection formData = new NameValueCollection();
formData["field-keywords"] = "Harry Potter";
// add more form field / values here
WebClient webClient = new WebClient();
byte[] responseBytes = webClient.UploadValues(url, "POST", formData);
string response = Encoding.UTF8.GetString(responseBytes);
Console.WriteLine(response);

MVC Razor String Concat

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")" />

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" />

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