As per this article I am trying to bind a list of non sequential items.
View:
<%using (Html.BeginForm("Products", "Home", FormMethod.Post))
{ %>
<input type="hidden" name="products.Index" value="cold" />
<input type="text" name="products[cold].Name" value="Beer" />
<input type="text" name="products[cold].Price" value="7.32" />
<input type="hidden" name="products.Index" value="123" />
<input type="text" name="products[123].Name" value="Chips" />
<input type="text" name="products[123].Price" value="2.23" />
<input type="hidden" name="products.Index" value="caliente" />
<input type="text" name="products[caliente].Name" value="Salsa" />
<input type="text" name="products[caliente].Price" value="1.23" />
<input type="submit" value="Submit" />
<%} %>
Action method:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Products(IList<Product> products)
{
return View("Index");
}
Binding doesn't seems to work for me, the parameter products always contains null. Am I missing something?
Any help much appreciated, Thanks.
Please note, I am using ASP.NET MVC 1.0
The default model binder is capable of binding collections with non-sequential indexes starting from ASP.NET MVC 2.0. This is not supported in ASP.NET MVC 1.0.
Related
I have a form that binds to three related models in a single EditForm. I am looking to understand how to validate each of them on the same submit. I have been able to successfully validate a single model, but I don't see any details anywhere on how to validate multiples. Ideas?
<EditForm OnValidSubmit="#Save" EditContext="#EditContext">
<div class="form-group">
<input class="form-control" type="text" id="Title" #bind="#TargetUser.Title" />
<InputText Id="OfficePhone" Class="form-control" #bind-Value="#TargetUser.OfficePhone"></InputText>
<ValidationMessage For="#(() => TargetUser.OfficePhone)" />
<input class="form-control" type="text" id="MiddleName" #bind="#TargetUser.MiddleName" />
<div class="row row-padding">
<h4>Seller Rates</h4>
</div>
<hr />
<input type="number" step="0.01" id="HourlyRate" #bind="#UserRate.HourlyRate" class="form-control" />
<input type="number" id="Salary" #bind="#UserRate.Salary" class="form-control" />
<input type="number" step="0.01" id="OTRate" #bind="#UserRate.OTRate" class="form-control" />
<input type="date" #bind="#UserRate.ValidFrom" id="ValidFrom" class="form-control"/>
<input type="date" class="form-control" id="ValidTo" #bind="#UserRate.ValidTo" />
<DataAnnotationsValidator />
<ValidationSummary />
</EditForm>
This is a highly edited example of some of the code. Not intended to show what would actually be there. Just to illustrate.
I guess what you need here is the ObjectGraphDataAnnotationsValidator component, which enables validation of complex types.
Here's a link to a simple sample
Here's a link to the class definition and samples by the Blazor team
Hope this helps...
<form action="#Url.Action("PokemonSteps", "PokemonView", new { id = Model.Id, steps = steps })">
<input type="number" name="steps" min="1" max="#Model.userSteps">
<input type="submit">
</form>
This form is a number box from 1 to the amount of steps the user has.
When they submit the number in the box, I want to pass it to #Url.Action as the steps in the steps = steps part.
How do I go about that (sorry for the really stupid question)
<form action="#Url.Action("PokemonSteps", "PokemonView")" method="post">
<input type="hidden" name="id" value="#Model.Id">
<input type="number" name="steps" min="1" max="#Model.userSteps">
<input type="submit" value="Submit">
</form>
Please add Attribute method="post" and share your controller method signature also...
Try doing something like this:
#using (Html.BeginForm("PokemonSteps", "PokemonView", FormMethod.Post)){
#Html.TextBoxFor(m=> m.userSteps)
<input type="submit" value="Submit" />
}
Hope this Helps!!
I'm building a webform in c# .net mvc using mongodb to store information. The form works with a company object that has a property that is a List of Addresses, called addressdata. When the form is submitted, the company object is sent to the controller and then upserted into MongoDB. The input names take the form
<input type="text" name="Company.addressdata[a].city" />
Where "a" is the index in the list. This all works great! The list of address objects is created upon submission and inserts into mongoDB.
However, I just added the ability to delete addresses, and now I'm running into trouble. I have noticed that when a user deletes the first row, all the rows after are lost. So, if they delete the 0 index, the Company object will not populate the list of Addresses and thus they will not go into MongoDB.
Is there a way to work around this? Is this how it's designed to work? It seems like too much to renumber all of the following rows with the new index, but is that what it takes? Or is there another way?
In my experience, that's by design. The indexes must start from 0, or you have to define your own indexes for each of them with a special element.
This article shows an example of that: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
<form method="post" action="/Home/Create">
<input type="hidden" name="products.Index" value="cold" />
<input type="text" name="products[cold].Name" value="Beer" />
<input type="text" name="products[cold].Price" value="7.32" />
<input type="hidden" name="products.Index" value="123" />
<input type="text" name="products[123].Name" value="Chips" />
<input type="text" name="products[123].Price" value="2.23" />
<input type="hidden" name="products.Index" value="caliente" />
<input type="text" name="products[caliente].Name" value="Salsa" />
<input type="text" name="products[caliente].Price" value="1.23" />
<input type="submit" />
</form>
So you have options, either:
Update indices when deleting, or
Define arbitrary indices
I am referring to this post from Phil Haack http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx I need to do the same but what if I need to insert records in the list, at any index I wanted? How do I keep the indexes in sync short of doing some Javascript and updating all indexes?
<form method="post" action="/Home/Create">
<input type="text" name="[0].Title" value="Curious George" />
<input type="text" name="[0].Author" value="H.A. Rey" />
<input type="text" name="[0].DatePublished" value="2/23/1973" />
<input type="text" name="[1].Title" value="Code Complete" />
<input type="text" name="[1].Author" value="Steve McConnell" />
<input type="text" name="[1].DatePublished" value="6/9/2004" />
<!-- I want to insert a record here by adding the fields through JavaScript -->
<input type="text" name="[2].Title" value="The Two Towers" />
<input type="text" name="[2].Author" value="JRR Tolkien" />
<input type="text" name="[2].DatePublished" value="6/1/2005" />
<input type="submit" />
I didn't quite understand how I would use the "Non-Sequential Indices" described on that blog post for doing stuff like inserting records to specific index.
Any ideas?
The WorldPay payment gateway suggests using this HTML to take the customer to the payment page:
<form action="https://select-test.wp3.rbsworldpay.com/wcc/purchase" name="BuyForm" method="POST">
<input type="hidden" name="instId" value="211616">
<input type="hidden" name="cartId" value="abc123">
<input type="hidden" name="currency" value="GBP">
<input type="hidden" name="amount" value="0">
<input type="hidden" name="desc" value="">
<input type="hidden" name="testMode" value="100">
<input type="submit" value="To Payment!">
</form>
How to I put this form on my page? The problem is I have a master page which wraps the content pages content in the ASP.net form, I can't nest the forms.
This is a bit of a hack, but I have used this with paypal in the past. Basiscally just put and extra form above the form you are attempting to post as in the following:
<form>
</form>
<form action="https://select-test.wp3.rbsworldpay.com/wcc/purchase" name="BuyForm" method="POST">
<input type="hidden" name="instId" value="211616">
<input type="hidden" name="cartId" value="abc123">
<input type="hidden" name="currency" value="GBP">
<input type="hidden" name="amount" value="0">
<input type="hidden" name="desc" value="">
<input type="hidden" name="testMode" value="100">
<input type="submit" value="To Payment!">
</form>
Enjoy!
You can customize the HtmlForm to get this to work. I gave a code sample here:
How to get past embedding a html form for paypal buttons asp.net
http://www.codersbarn.com/post/2008/03/08/Solution-to-ASPNET-Form-PayPal-Problem.aspx
I've used this several times with master pages :-)