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?
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...
I'm using the following code to create a payment button and am modifying the values for amount,currency_code and custom dynamically:
<input type="hidden" name="business" value="test1#example.com" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="item_name" value="test payment" />
<input type="hidden" name="amount" value="0" />
<input type="hidden" name="custom" value="0" />
<input type="hidden" name="notify_url" value="http://www.example.com/IPN.aspx" />
<input type="hidden" name="return" value="http://www.example.com/Thanks.aspx" />
<input type="hidden" name="lc" value="GB">
<input type="hidden" name="item_name" value="test app">
<input type="hidden" name="button_subtype" value="services">
I'm passing the productID of the product im selling in 'custom' and then I'm then doing some checks in my IPN.aspx to check that the values for amount and currency have not been modified by using the ProductID to get details of the product price & currency from my database and comparing this to what is returned by paypal. Surely this is secure because if the details don't match in my IPN the payment is not recorded?
I've looked around for answers to this and people just say that it's not secure because you can use fiddler/tamperdata etc to change the values of the amount or currency code but surely if I'm checking in the IPN then this is secure. What are the security risks of doing the payment this way and is there any way to hack or abuse it?
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 have two divs in separate form tags. They render correctly in chrome and firefox but unfortunately not in IE. The alignment of the divs get distorted. If it remove the first form tag from the code it solves the alignment issue but I am not able to postback in the link in the second form tag.Hence I need to separate both the divs in two different form tags. Can somebody help
<form>
<div id="FundsNetworkNotAvailable" class="msgError" runat="server">
<%--These date is dynamically pulled from the config file--%>
<h2>Attention</h2>
<p>
FundsNetwork will be carrying out maintenance work on their website over the weekend from
<asp:Label runat="server" ID="FundsNetworkNotAvailableFrom" Text="19/12/2009" />
to
<asp:Label runat="server" ID="FundsNetworkNotAvailableUntil" Text="20/12/2009" />.
During this period, their website will become unavailable.
We apologise for the disruption and any inconvenience caused.
</p>
</div>
<br />
</form>
</form>
<form id="post_info_to_Fidelity" name="post_info_to_Fidelity" method="post" target="_blank" action="https://www.fidelity.co.uk/investor/site/general/jsp/general_set_cookie.jsp">
<input name="page" value="isaPlusAO" type="hidden">
<input type="hidden" name="WLSEDOLS" value="">
<input type="hidden" name="WLSEDOLS_INSIDE_ISA_VALUES" value="">
<input type="hidden" name="WLSEDOLS_OUTSIDE_ISA_VALUES" value="">
<input name="oafRate" value="0.45" type="hidden">
<input name="vatable" value="false" type="hidden">
<input name="Advicegiven" value="no" type="hidden">
<input name="partner" value="best" type="hidden">
<input name="Context" value="isaPlusAO" type="hidden">
<input name="IGNORE_RemoveUserFromSession" value="yes" type="hidden">
<input name="isNewClient" value="true" type="hidden">
<input name="AdviserClient" value="1" type="hidden">
<input name="AgentCode" value="170224" type="hidden">
<input name="returnURL" value="http://select.bestinvest.co.uk" type="hidden">
<input name="Title" value="" type="hidden">
<input type="hidden" name="Surname" size="25" maxlength="25" value="">
<input type="hidden" name="FirstNames" size="25" maxlength="25" value="">
<input type="hidden" name="HouseName" size="25" maxlength="25" value="">
<div class="ColSplit Third inset insetThird" style="border-bottom: 2px solid #E1E1E1;">
<h2 class="NoBorder">Select an investment option with FundsNetwork</h2>
<p>
To transact through FundsNetwork you will firstly need to login to your
<a href="https://www.fidelity.co.uk/investor/site/general/jsp/general_set_cookie.jsp"
title="Link to FundsNetwork account manager. (opens in a new window)"
target="_blank">FundsNetwork Account Manager </a>.
To access this you will need your
FundsNetwork Customer Reference Number (a ten digit number beginning with ‘1’
that can found on your most recent statement from FundsNetwork) and pin. Please
call FundsNetwork on 0800 995511 if you need help logging in to the account manager.
</p>
<div class="cta fRight">
<input title="Link to FundsNetwork account manager. (opens in a new window)" type=submit value=Go class="button">
</div>
</div>
</form>
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.