ارسال لیستی از Customer ها به از Veiw به Controller

ساخت وبلاگ

چهارشنبه 22 آذر 1396, 20:00 عصر #1

ارسال لیستی از Customer ها به از Veiw به Controller

با سلام دوستان من صفحه ای دارم که میخوام مثلا تعداد پنچ تا مشتری رو با کلیک روی یک دکمه ارسال کنم به لایه کنترلر
میخواستم ببینم چطوری این کارو انجام بدم من یک مشتری رو میتونم ارسال کنم .. اما چند تا رو نمیدونم .

Post.PNG

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>

آخرین ویرایش به وسیله مهیار. : چهارشنبه 22 آذر 1396 در 20:14 عصر

چهارشنبه 22 آذر 1396, 20:42 عصر #2

نقل قول: ارسال لیستی از Customer ها به از Veiw به Controller

وقتی Input میسازید قسمت name باید دارای یک اندیس باشه.

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

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

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


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

برنامه نویس...
ما را در سایت برنامه نویس دنبال می کنید

برچسب : نویسنده : محمد رضا جوادیان programers بازدید : 168 تاريخ : پنجشنبه 23 آذر 1396 ساعت: 1:28

خبرنامه