استخراج داده با Ajax از partialview و قرار دادن آن در المان html
در این مثال وقتی فرم RecordP مخفی می شود تابع Ajax اجرا شده و مقادیر را از جدول خوانده و آن را مجددا به روز می کند.
<script type="text/javascript">
$(document).ready(function () {
$("#RecordP").on("hide.bs.modal", function () {
$.ajax({
type: "GET",
url: '/FBProject/GetAllProjectPartial',
//data: formData,
contentType: false,
processData: false,
dataType: "html",
cache: false,
success: successFunc,
error: errorFunc,
complete: completeFunc
});
function successFunc(data)
{
$('#mainpage').html(data);
}
function errorFunc() { alert("خطا در آپلود فایل"); }
function completeFunc() { $('#loading').remove(); }
});
});
</script>
باند کردن مدل به DropDownList درMVC
کدهای کنترلر برای فرستادن داده های مدل
public ActionResult Create()
{
ViewBag.FBAbnieVahedID = new SelectList(db.FBAbnieVahed, "FBAbnieVahedID", "FBAbnieVahedName");
return View();
}
کدهای view برای نمایش داده ها :
<div class="form-group">
@Html.LabelFor(model => model.FBAbnieVahedID, "واحد", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("FBAbnieVahedID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.FBAbnieVahedID, "", new { @class = "text-danger" })
</div>
</div>
پارمتر اول Html.DropDownList که به صورت String می باشد مشخص کننده نام پارامتر ViewBag ی می باشد که داده ها مدل توسط آن ارسال شده است.
@Html.DropDownList("MachBrandID", (SelectList)ViewBag.MyList, "انتخاب کنید", htmlAttributes: new { @class = "form-control" })
ست کردن آی دی برای کنترل
@Html.DropDownList("MachBrandID", null, htmlAttributes: new { @class = "form-control", id = "mbrand" })
مقدار دهی کنترل از طریق آی دی :
$("document").ready(function () {
$('#mbrand').val('@Model.MachBrandID');
});
روش انتقال داده بین View و Controller
public ActionResult Index01()
{
ViewData["hh"] = " Hello Godarzi";
return View();
}
برداشت متغییر رشته :
<body class="container">
<div>@ViewData["hh"]</div>
یا
<div>@ViewBag.hh</div>
</body>
namespace MVCView.Models
{
public class Person
{
public int ID { get; set; }
public String FullName { get; set; }
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public ActionResult Index02()
{
Person mPerson = new Person()
{
ID = 12,
FullName ="Salman"
};
ViewBag.myPeron = mPerson;
return View();
}
دریافت :
<h1>@ViewBag.myPeron.ID</h1>
<h1>@ViewBag.myPeron.FullName</h1>
روش دوم دریافت شی :
@{
MVCView.Models.Person Mpeson = ViewBag.myPeron as MVCView.Models.Person;
<h1>-------------------------</h1>
<h3>@Mpeson.ID</h3>
<h3>@Mpeson.FullName</h3>
}
public ActionResult Index03()
{
Person mPerson = new Person()
{
ID = 12,
FullName = "Salman"
};
return View(mPerson);
}
دریافت :
@model MVCView.Models.Person
@{
ViewBag.Title = "Index03";
}
<h2>Index03</h2>
<h1>-------------------------</h1>
<h3>ID : @Model.ID </h3>
<h3>FullName : @Model.FullName</h3>
روش چهارم استفاده از ViewModels : برا ی این کار ابتدا یک پوشه (فضا نام) به نام ViewModels در پروژه ایجاد کرده و کلیه کلاس ها یا متغغیرهایی که نیاز است در View استفاده شود به صورت یک کلاس در آن تعریف میکنیم و آن را برای View ارسال می کنیم :
کلاس مدل برای View
namespace MVCView.ViewMidels
{
public class Factory
{
public String FactoryName { get; set; }
public Person Person { get; set; }
}
}
تعریف اکشن :
public ActionResult Index04()
{
Person mPerson = new Person()
{
ID = 12,
FullName = "Salman"
};
Factory factory = new Factory()
{
FactoryName ="Kia Sport",
Person = mPerson
};
return View(factory);
}
تعریف view
@model MVCView.ViewMidels.Factory
@{
ViewBag.Title = "Index04";
}
<h2>Index04</h2>
<h1>-------------------------</h1>
<h3>Factory : @Model.FactoryName </h3>
<h1>-------------------------</h1>
<h3>ID : @Model.Person.ID </h3>
<h3>FullName : @Model.Person.FullName</h3>
public ActionResult Index05()
{
List<Person> Lperon = new List<Person>()
{
new Person(){ID=1,FullName="Ali"},
new Person(){ID=2,FullName="Hassan"},
new Person(){ID=3,FullName="Salman"},
new Person(){ID=4,FullName="Sepehr"}
};
return View(Lperon);
}
دریافت در View
@model List<MVCView.Models.Person>
@{
ViewBag.Title = "Index05";
}
<h2>Index05</h2>
@{
if(Model.Count!=0)
{
foreach(var pp in Model)
{
<h5>ID: @pp.ID</h5>
<h5>Name: @pp.FullName</h5>
<hr/>
}
}
}
فعال کردن Web API هنگام استفاده اجرای برنامه از طریق Startup
برای این کار ابتدا باید پکیج زیر را نصب کنیم
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
سپس کد های زیر را به متد Configuration کلاس Startup اضافه کنیم
[assembly: OwinStartup(typeof(IdentitySample.Startup))]
namespace IdentitySample
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
ConfigureAuth(app);
}
}
}
تنظیمWEB API برای ایجاد خروجی به صورت JSON
روش اول
برای این کار کدهای زیر را به قسمت Global.asax اضافه نمایید:
GlobalConfiguration.Configuration.Formatters.
JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters
.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
روش دوم :
یا اضافه کردن کد زیر به App_Start / WebApiConfig.cs
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
روش نصب پکیج ها ویژال استودیو به صورت آفلاین
برای این کار از دستور زیر در خط دستوری نیوگیت استفاده کنید
Install-Package C:\Path\To\Some\File.nupkg
objWebClient = new WebClient();
objWebClient.Headers[HttpRequestHeader.ContentType] = "application/json ; charset=utf-8";
objWebClient.Encoding = UTF8Encoding.UTF8;