مرجع مقالات کاربردی طراحی اپلیکیشن

شرح کامل مقالات طراحی اپلیکیشن

مرجع مقالات کاربردی طراحی اپلیکیشن

شرح کامل مقالات طراحی اپلیکیشن

شرح کامل مقالات طراحی اپلیکیشن

  • ۰
  • ۰

کلاس Model

اذن دهید یک کلاس موجودیت به طراحی اپلیکیشن در مشهد اسم “Customer” رایا پارس بسازیم application که با شمای(schema) جدول Customers در مقر داده متناسب باشد.

public class Customer
{
[BsonId]
public ObjectId Id { get; set; }
[BsonElement]
public int CustomerId { get; set; }
[BsonElement]
public string CustomerName { get; set; }
[BsonElement]
public string Address { get; set; }
}
کلاس دربرگیرنده ویژگی Id از نوع ObjectId میباشد این ویژگی برای تطبیق یک مورد در collection های MongoDB به کار گیری می‌شود. ما همینطور یک خصوصیت دیگر به اسم BsonElement داریم که برای نماد دادن یک عنصر در collection های MongoDB به شغل گرفته می‌گردد.

روال های Controller

در Controller ما کد هایی برای تلاوت ، بازنویسی، تشکیل داد و حذف رکورد ها از MongoDB اضافه خوا هیم کرد.کدها را برای بازیابی جزئیات مقر داده به یک اسلوب معمول منتقل کرده ایم.

public class HomeController : Controller
{
private IMongoDatabase mongoDatabase;

//Generic method to get the mongodb database details
public IMongoDatabase GetMongoDatabase()
{
var mongoClient = new MongoClient(\"mongodb://localhost:27017\");
return mongoClient.GetDatabase(\"CustomerDB\");
}

[HttpGet]
public IActionResult Index()
{
//Get the database connection
mongoDatabase = GetMongoDatabase();
//fetch the details from CustomerDB and pass into view
var result = mongoDatabase.GetCollection(\"Customers\").Find(FilterDefinition.Empty).ToList();
return View(result);
}

[HttpGet]
public IActionResult Create()
{
return View();
}

[HttpPost]
public IActionResult Create(Customer customer)
{
try
{
//Get the database connection
mongoDatabase = GetMongoDatabase();
mongoDatabase.GetCollection(\"Customers\").InsertOne(customer);
}
catch (Exception ex)
{
throw;
}
return RedirectToAction(\"Index\");
}

[HttpGet]
public IActionResult Details(int? id)
{
if (id == null)
{
return NotFound();
}
//Get the database connection
mongoDatabase = GetMongoDatabase();
//fetch the details from CustomerDB and pass into view
Customer customer = mongoDatabase.GetCollection(\"Customers\").Find(k => k.CustomerId == id).FirstOrDefault();
if (customer == null)
{
return NotFound();
}
return View(customer);
}

[HttpGet]
public IActionResult Delete(int? id)
{
if (id == null)
{
return NotFound();
}
//Get the database connection
mongoDatabase = GetMongoDatabase();
//fetch the details from CustomerDB and pass into view
Customer customer = mongoDatabase.GetCollection(\"Customers\").Find(k => k.CustomerId == id).FirstOrDefault();
if (customer == null)
{
return NotFound();
}
return View(customer);
}

[HttpPost]
public IActionResult Delete(Customer customer)
{
try
{
//Get the database connection
mongoDatabase = GetMongoDatabase();
//Delete the customer record
var result = mongoDatabase.GetCollection(\"Customers\").DeleteOne(k => k.CustomerId == customer.CustomerId);
if (result.IsAcknowledged == false)
{
return BadRequest(\"Unable to Delete Customer \" + customer.CustomerId);
}
}
catch (Exception ex)
{
throw;
}
return RedirectToAction(\"Index\");
}

[HttpGet]
public IActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
//Get the database connection
mongoDatabase = GetMongoDatabase();
//fetch the details from CustomerDB based on id and pass into view
var customer = mongoDatabase.GetCollection(\"Customers\").Find(k => k.CustomerId == id).FirstOrDefault();
if (customer == null)
{
return NotFound();
}
return View(customer);
}

[HttpPost]
public IActionResult Edit(Customer customer)
{
try
{
//Get the database connection
mongoDatabase = GetMongoDatabase();
//Build the where condition
var filter = Builders.Filter.Eq(\"CustomerId\", customer.CustomerId);
//Build the update statement
var updatestatement = Builders.Update.Set(\"CustomerId\", customer.CustomerId);
updatestatement = updatestatement.Set(\"CustomerName\", customer.CustomerName);
updatestatement = updatestatement.Set(\"Address\", customer.Address);
//fetch the details from CustomerDB based on id and pass into view
var result = mongoDatabase.GetCollection(\"Customers\").UpdateOne(filter, updatestatement);
if (result.IsAcknowledged == false)
{
return BadRequest(\"Unable to update Customer \" + customer.CustomerName);
}
}
catch (Exception ex)
{
throw;
}

return RedirectToAction(\"Index\");
}

public IActionResult About()
{
ViewData[\"Message\"] = \"Your application description page.\";

return View();
}

public IActionResult Contact()
{
ViewData[\"Message\"] = \"Your contact page.\";

return View();
}

public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
کد برای View های MVC

از آنجایی که‌این نوشته ی علمی چیزی بیش تر از یک دمو از MongoDB میباشد.از آیتم ی scaffolding که در MVC برای تشکیل داد View دردسترس میباشد به کارگیری کرده ایم. شما می‌توانید این گزینه را براساس نیاز های خویش تغییر و تحول دهید.

InDEX VIEW

@model IEnumerable
@{
ViewData[\"Title\"] = \"Index\";
}
Index


Create New

@foreach (var item in Model)
{


}


@Html.DisplayNameFor(model => model.CustomerId)

@Html.DisplayNameFor(model => model.CustomerName)

@Html.DisplayNameFor(model => model.Address)
Actions

@Html.DisplayFor(modelItem => item.CustomerId)

@Html.DisplayFor(modelItem => item.CustomerName)

@Html.DisplayFor(modelItem => item.Address)

@Html.ActionLink(\"Edit\", \"Edit\", new { id = item.CustomerId }) |
@Html.ActionLink(\"Details\", \"Details\", new { id = item.CustomerId }) |
@Html.ActionLink(\"Delete\", \"Delete\", new { id = item.CustomerId })

VIEW ایجاد کرد

@model AspNetCoreMVCMongoDBDemo.Models.Customer
@{
ViewData[\"Title\"] = \"Create\";
}
Create Customer Details


\"Create\"


Back to List

@section Scripts {
@{await Html.RenderPartialAsync(\"_ValidationScriptsPartial\");}
}
VIEW حذف

@model AspNetCoreMVCMongoDBDemo.Models.Customer
@{
Layout = \"_Layout\";
}
Delete Customer


Are you sure to delete 


\"Delete\"


Back to List

VIEW جزئیات

@model AspNetCoreMVCMongoDBDemo.Models.Customer
@{
ViewData[\"Title\"] = \"Details\";
}

Customer Details


@Html.DisplayNameFor(model => model.CustomerId)


@Html.DisplayFor(model => model.CustomerId)


@Html.DisplayNameFor(model => model.CustomerName)


@Html.DisplayFor(model => model.CustomerName)


@Html.DisplayNameFor(model => model.Address)


@Html.DisplayFor(model => model.Address)

Back to List

VIEW بازنویسی

@model AspNetCoreMVCMongoDBDemo.Models.Customer
@{
Layout = \"_Layout\";
}
@{
ViewData[\"Title\"] = \"Details\";
}
Edit Customer Details

\"Save\"


Back to List

شما می‌توانید source code برای Asp.NetCoreMVCMongoDBDemo را از GitHub (پیوند)دانلود نمائید.

  • ۰۱/۰۶/۰۷
  • شایلین عباسی

طراحی اپلیکیشن در مشهد

نظرات (۰)

هیچ نظری هنوز ثبت نشده است

ارسال نظر

ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی