I have a store of products. Each product has an Id, a price and a name. When a person clicks on the "Buy" button, a form opens so they can fill up their address, their email and their name. When the person clicks on submit, I do an AJAX call to the controller sending the information.
I have tried several solutions but I can't find one that works :/ What I want to do is send the email, address, name and an array of products.
carrinho.js (shoppingcart.js):
function go(nr) {
$('.confirm').toggleClass('comein');
$('#shoppingCart').toggle("fade");
var i = 0;
$("div #cartItems > .itemrow").each(function () {
var nome = $(this).find("h3").text();
var preco = $(this).find(".price").text();
var id = $(this).find(".id").text();
$("#form").append("" +
"<input type='hidden' name='[" + i + "].produtos' value='" + nome + "' id='nomeenc' readonly /> " +
"<input type='hidden' name='[" + i + "].produtos' value='" + id + "' id='idenc' readonly />" +
"<input type='hidden' name='[" + i + "].produtos' value='" + preco + "' id='precoenc' readonly />");
$(".messageSuccess button .removeItem").remove();
$(".messageSuccess img").remove();
i = i + 1;
});
$("form").on("submit", function (e) {
e.preventDefault();
var form = $(this);
$.ajax({
type: 'POST',
url: '/Compra/Encomenda',
data: form.serialize(),
success: function () {
$('.confirm').toggleClass('comein');
$('#emptyCart').fadeToggle(nr);
$('.messageSuccess').toggleClass('comein');
}
});
});
}
Controller:
public ActionResult Encomenda(SubmitFormModel produtos)
{
var model = produtos;
string path = Server.MapPath("~/files");
DateTime time = DateTime.Now;
string date = time.ToString("yyyy-MM-dd-hh-mm-ss");
XmlSerializer serializer = new XmlSerializer(typeof(SubmitFormModel));
StreamWriter writer = new StreamWriter(path + "\\" + date + ".xml");
serializer.Serialize(writer, model);
writer.Close();
return PartialView(model);
}
SubmitFormModel.cs:
namespace Loja.Models
{
[Serializable]
public class SubmitFormModel
{
public List<Produto> Produtos { get; set; } //produto = product
public string Morada { get; set; } //address
public string Email { get; set; }
public string Nome { get; set; } //name
}
}
Produto.cs:
namespace Loja.Models
{
[Serializable]
public class Produto
{
public int Id { get; set; }
public string Nome_produto { get; set; } //product_name
public string Tipo { get; set; } //type
public string Detalhes { get; set; } //details
public string Imagem { get; set; } //image
public int Preco { get; set; } //price
public string ImgArt { get; set; }
}
}
Form:
<form action="/Compra/Encomenda" method="post" id="form">
<label class="field a-field a-field_a3 page__field">
<input class="field__input a-field__input" placeholder="ex. Rodrigo Barradinhas" required name="nome" type="text" id="nome_cliente">
<span class="a-field__label-wrap">
<span class="a-field__label">Nome completo</span>
</span>
</label>
<label class="field a-field a-field_a3 page__field">
<input class="field__input a-field__input" placeholder="ex. [email protected]" required name="email" type="text" id="email">
<span class="a-field__label-wrap">
<span class="a-field__label">E-mail</span>
</span>
</label>
<label class="field a-field a-field_a3 page__field">
<input class="field__input a-field__input" placeholder="ex. Rua x Nºy 1167-004 Lisboa" required name="morada" type="text" id="morada">
<span class="a-field__label-wrap">
<span class="a-field__label">Morada</span>
</span>
</label>
<input type='submit' name='confirmar' value='confirmar' class='event' data-main='Compra' />
<br /><br />
</form>
I wanted to get in my controller something like: https://i.sstatic.net/3nJMv.jpg
and an array of products
name='produtos[" + i + "].name'instead ofname='[" + i + "].produtos'? With one input per property like you do but withnameequals toprodutos[" + i + "].**propertyName**.eachmethod you're setting the sameidto severalinputwhich is not a good idea asidsshould be unique. I'd suggest you remove then if you don't need them.produtos[" + i + "].nome_produtoinstead ofnometo match theProdutomodel but other than that it's strange that the other properties aren't set anymore.