نیکناز
یک شنبه 17 شهریور 1392, 21:19 عصر
سلام
من می خوام با زدن یه دکمه ، اول یه سری از فیلد های مدلم با ( td های تو پارتیشال ویو) پر بشن ، بعد عمل پست انجام بگیره .
خودم فکر می کنم تو کنترلر اشتباه میکنم .
میشه [HttpGet] از نوع ActionResult باشه ولی [HttpPost] از نوع JsonResult .
نام هاشون هم فرق کنه؟
بخش JsonResult رو درست می نویسم؟
کنترلر:
Mycontroller:
[HttpGet]
public ActionResult Customer()
{
var obj = new Project.Models.CentricModelClasses.ViewModelX();
return PartialView(obj);
}
[HttpPost]
public JsonResult Save(ViewModelX jsonMyModel)
{
var result = true;
if (ModelState.IsValid)
{
result= MyClass.Insert (jsonMyModel.Address, jsonMyModel.Cod,
jsonMyModel.idpurchase,
jsonMyModel.idspend, jsonMyModel.Lname,
jsonMyModel.Name, jsonMyModel.Tell, jsonMyModel.username);
}
else
{
}
return Json(result, JsonRequestBehavior.AllowGet);
}
کلاس :
public class ViewModelX
{
public Nullable< long > idpurchase { get; set; }
public Nullable<long> idspend { get; set; }
public string username { get; set; }
public string Name { get; set; }
public string Lname { get; set; }
public string Tell { get; set; }
public string Address { get; set; }
public string CodPosti { get; set; }
}
پارتیشال ویو :
@Html.HiddenFor(m => m.username)
@Html.HiddenFor(m => m.Tell)
@Html.HiddenFor(m => m.Name)
@Html.HiddenFor(m => m.Mobil)
@Html.HiddenFor(m => m.Lname)
<script type="text/javascript">
$("#submit-btn2").click(function () { saveMyModel();});
function SaveMyModel()
{
var e = document.getElementById("id_purchase");
var str = e.options[e.selectedIndex].value;
var e2 = document.getElementById("id_spend");
var str2 = e.options[e.selectedIndex].value;
$.ajax({
url: '@Url.Action("Save", "Home")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
jsonMyModel: {
username: $(".tdBuyername").val(),
Tell: $(".tdPhone").val(),
Name: $(".tdRecievername").val(),
Mobil: $(".tdMobile").val(),
Lname: $(".tdLname").val(),
id_purchase: $("# id_purchase ").val(str),
id_spend: $("# id_spend ").val(str2),
}
})
});
}
</script>
من می خوام با زدن یه دکمه ، اول یه سری از فیلد های مدلم با ( td های تو پارتیشال ویو) پر بشن ، بعد عمل پست انجام بگیره .
خودم فکر می کنم تو کنترلر اشتباه میکنم .
میشه [HttpGet] از نوع ActionResult باشه ولی [HttpPost] از نوع JsonResult .
نام هاشون هم فرق کنه؟
بخش JsonResult رو درست می نویسم؟
کنترلر:
Mycontroller:
[HttpGet]
public ActionResult Customer()
{
var obj = new Project.Models.CentricModelClasses.ViewModelX();
return PartialView(obj);
}
[HttpPost]
public JsonResult Save(ViewModelX jsonMyModel)
{
var result = true;
if (ModelState.IsValid)
{
result= MyClass.Insert (jsonMyModel.Address, jsonMyModel.Cod,
jsonMyModel.idpurchase,
jsonMyModel.idspend, jsonMyModel.Lname,
jsonMyModel.Name, jsonMyModel.Tell, jsonMyModel.username);
}
else
{
}
return Json(result, JsonRequestBehavior.AllowGet);
}
کلاس :
public class ViewModelX
{
public Nullable< long > idpurchase { get; set; }
public Nullable<long> idspend { get; set; }
public string username { get; set; }
public string Name { get; set; }
public string Lname { get; set; }
public string Tell { get; set; }
public string Address { get; set; }
public string CodPosti { get; set; }
}
پارتیشال ویو :
@Html.HiddenFor(m => m.username)
@Html.HiddenFor(m => m.Tell)
@Html.HiddenFor(m => m.Name)
@Html.HiddenFor(m => m.Mobil)
@Html.HiddenFor(m => m.Lname)
<script type="text/javascript">
$("#submit-btn2").click(function () { saveMyModel();});
function SaveMyModel()
{
var e = document.getElementById("id_purchase");
var str = e.options[e.selectedIndex].value;
var e2 = document.getElementById("id_spend");
var str2 = e.options[e.selectedIndex].value;
$.ajax({
url: '@Url.Action("Save", "Home")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
jsonMyModel: {
username: $(".tdBuyername").val(),
Tell: $(".tdPhone").val(),
Name: $(".tdRecievername").val(),
Mobil: $(".tdMobile").val(),
Lname: $(".tdLname").val(),
id_purchase: $("# id_purchase ").val(str),
id_spend: $("# id_spend ").val(str2),
}
})
});
}
</script>