PDA

View Full Version : ارسال لیستی از Customer ها به از Veiw به Controller



مهیار.
چهارشنبه 22 آذر 1396, 19:00 عصر
با سلام دوستان من صفحه ای دارم که میخوام مثلا تعداد پنچ تا مشتری رو با کلیک روی یک دکمه ارسال کنم به لایه کنترلر
میخواستم ببینم چطوری این کارو انجام بدم من یک مشتری رو میتونم ارسال کنم .. اما چند تا رو نمیدونم .

147252

Controller :




public ActionResult SaveCustomer(List<Customer> items)
{
var Db = new DataBase();
foreach (var item in items)
{
Customer cust = new Customer() { NameCustomer = item.NameCustomer, FamilyCustomer=item.FamilyCustomer,Tel=item.Tel };
Db.Customers.Add(cust);
Db.SaveChanges();
int CustomserID = cust.Id;


CustomerProduct CuPr = new CustomerProduct() { ProductId = _productID, CustomerId = CustomserID };
Db.CustomerProducts.Add(CuPr);
Db.SaveChanges();
}



View :



<div style="padding-top:30px;padding-right:20px">


<div>
<div>
نام
<input class="input-medium" style="margin-left:10px" id="NameCustomer" type="text" />
نام خانوادگی :
<input class="input-medium" style="margin-left:10px" id="FamilyCustomer" type="text" />
شماره تلفن :
<input class="input-medium" style="margin-left:10px" id="TelCustomer" type="text" />
<br />
</div>


<div>
نام
<input class="input-medium" style="margin-left:10px" id="NameCustomer1" type="text" />
نام خانوادگی :
<input class="input-medium" style="margin-left:10px" id="FamilyCustomer1" type="text" />
شماره تلفن :
<input class="input-medium" style="margin-left:10px" id="TelCustomer1" type="text" />
<br />
</div>


</div>
<input class="btn btn-large btn-success" style="margin-right:550px;margin-top:45px" type="button" onclick="Save()" value="ثبت" />
</div>


JQuery :



<script>
function Save() {


var Customer1 = {
NameCustomer: $('#NameCustomer').val(),
FamilyCustomer: $('#FamilyCustomer').val(),
TelCustomer: $('#TelCustomer').val(),
};
var Customer2 = {
NameCustomer: $('#NameCustomer1').val(),
FamilyCustomer: $('#FamilyCustomer1').val(),
TelCustomer: $('#TelCustomer1').val(),
};


var Data = {
Cust1: Customer1,
Cust2: Customer2
}
$.ajax({
type: "POST",
url: "/Product/SaveCustomer",
data: JSON.stringify(Data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("success");
},
error: function (response) {
alert(response.responseText);
}
});
}
</script>

hakim22
چهارشنبه 22 آذر 1396, 19:42 عصر
وقتی Input میسازید قسمت name باید دارای یک اندیس باشه.


NewCustomer[1]
NewCustomer[2]
NewCustomer[3]
...


سیستم ModelBinding این اندیس هارو به آرایه تبدیل میکنه.
در این روش نیاز به هیچ کد جاوا اسکریپت ندارید.

راه دیگه استفاده از ajax و استفاده از json هست. باید آرایه از Customer ها درست کنید و بعد ارسال کنید.


Date={[Customer1,Customer2,...]}

مهیار.
پنج شنبه 23 آذر 1396, 09:13 صبح
ممنون .. میشه یه مثل بزنید .. من الان به صورت ارسال کردم ولی چیزی ارسال نشد ...



public ActionResult SaveCustomer(Customer[] items)
{


}




function Save() {


var Customer1 = {
NameCustomer: $('#NameCustomer').val(),
FamilyCustomer: $('#FamilyCustomer').val(),
TelCustomer: $('#TelCustomer').val(),
};
var Customer2 = {
NameCustomer: $('#NameCustomer1').val(),
FamilyCustomer: $('#FamilyCustomer1').val(),
TelCustomer: $('#TelCustomer1').val(),
};


var Data = {
Customer1,
Customer2
}
$.ajax({
type: "POST",
url: "/Product/SaveCustomer",
data: JSON.stringify(Data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("success");
},
error: function (response) {
alert(response.responseText);
}
});


}

hakim22
پنج شنبه 23 آذر 1396, 12:27 عصر
<input name="NewCustomer[1]" type="text" />
<input name="NewCustomer[2]" type="text" />
<input name="NewCustomer[3]" type="text" />

مهیار.
چهارشنبه 29 آذر 1396, 16:36 عصر
نه مشکل من اینه چنتا آبجکت ( مثلا مشتری و کالا ، ... ) میخوام باهم بفرستم سمت سرور ( کنترلر ) .. اما به اینصورت چیزی سمت سرور منتقل نمیشه



147302