以下是该场景:我正在编写一个MVC Web应用程序来跟踪我工作的公司的库存。所有数据都通过外部API访问。我首先从位于App_Start文件夹中的类API_Config.cs中实例化一个Connection对象。
using Moraware.JobTrackerAPI4;
namespace MVCWebgridExample.App_Start
{
public class API_Config
{
public static void ConnectToAPI()
{
//set up connection to API
var db = new Connection("https://databasename.somecompany.net/databasename/api.aspx", "Username", "Password");
db.Connect();
}
}
我的库存模型:
using System.Collections.Generic;
using Moraware.JobTrackerAPI4;
using System.Data.Entity;
using System.Web.Mvc;
namespace MVCWebgridExample.Models
{
public class InventoryViewModel
{
public List<string> locationList { get; set; }
public string stringLocation { get; set; }
bool LocationExists { get; set; }
public List<SerialNumber> SerialList { get; set; }
public string SerialName { get; set; }
//strings for button labels in initial prompt
public string lblScanIn { get; set; }
public string lblScanOut { get; set; }
}
public class ScanViewModel
{
public string LocationName { get; set; }
public List<InventoryLocation> LocationListFull { get; set; }
public List<SerialNumber> SerialsInLocation { get; set; }
public IEnumerable<string> SelectedSerial { get; set; }
public bool LocationExists { get; set; }
public static ScanViewModel ScanIn()
{
var db = new Connection("https://evolvegranite.moraware.net/evolvegranite/api.aspx", "Craig", "December12012");
db.Connect();
var svm = new ScanViewModel
{
LocationListFull =db.GetInventoryLocations(),
SerialsInLocation = db.GetSerialNumbers(new SerialNumberFilter(), new PagingOptions(0, 999, true)),
};
return svm;
}
}
我的观点:
@model MVCWebgridExample.Models.ScanViewModel
@using Moraware.JobTrackerAPI4
@{
ViewBag.Title = "ScanIn";
}
<div>
@using (Html.BeginForm(null, null, FormMethod.Get, new { name = "frmSerialsFromLocation", id = "frmSerialsFromLocation" }))
{
//@Html.ValidationSummary() Add this later by providing metadata to a strongly-typed viewmodel
<fieldset>
<legend>Scan an Inventory Location</legend>
<div>
<input type="text" name="tbLocation" autofocus />
</div>
<div>
<ul>
@if (ViewBag.serials != null)
{
foreach (var s in Model.SerialsInLocation)
{
<li>@s</li>}
}
</ul>
</div>
</fieldset>
<input type="submit" value="Scan!" />
}
</div>
控制器:
using System.Linq;
using System.Web.Mvc;
using Moraware.JobTrackerAPI4;
using System.Reflection;
using System.Collections.Generic;
using MVCWebgridExample.Models;
using System.Text;
namespace MVCWebgridExample.Controllers
{
public class InventoryController : Controller
{
//set up connection to API
Connection db = new Connection("https://evolvegranite.moraware.net/evolvegranite/api.aspx", "Craig", "December12012");
// Registering mapping
//public void RegisterMapping()
//{
// // Signature is the same for both type of objects
// Mapper.Register<Connection,mappedConnection>();
//}
// GET: Inventory
public ActionResult Index()
{
db.Connect();
return View();
}
// GET: /Inventory/ScanIn
//[HttpGet]
public ActionResult ScanIn(string stringlocation)
{
//var ScanIn = new ScanViewModel(); //create new instance of viewmodel
//var listSelectListItems = new List<SelectListItem>();
db.Connect();
if (stringlocation != null) //if a location has been scanned
{
var locationname = stringlocation; //set the location to a string
var locationList = db.GetInventoryLocations(); //get a list of ALL locations
var locationExists = locationList.Exists(x => x.InventoryLocationName == locationname);
if (locationExists)
{ //get full list of serials
var seriallist = db.GetSerialNumbers(new SerialNumberFilter(), new PagingOptions(0, 999, true));
var serialsinlocation = GetSerialsFromLocation(locationname); //get serials from location
List<SerialNumber> SerialsInLocation = new List<SerialNumber>(serialsinlocation);
foreach (var item in SerialsInLocation)
{
db.UpdateSerialNumber(item);
}
ViewBag.serials = SerialsInLocation;
return View(SerialsInLocation);
}
else return View(); //invalid location
}
else return View(); //do nothing because a location has not been scanned
}
// POST: /Inventory/ScanIn
//[HttpPost]
//public ActionResult ScanIn(string stringlocation)//receives the posted information by form
//{
// if (ModelState.IsValid)
// {
// if (stringlocation != null)
// {
// db.Connect();
// var locationname = stringlocation;
// var locationList = db.GetInventoryLocations();
// var locationExists = locationList.Exists(x => x.InventoryLocationName == locationname);
// var seriallist = db.GetSerialNumbers(new SerialNumberFilter(), new PagingOptions(0, 999, true));
// var serialsfromlocation = GetSerialsFromLocation(locationname);
// foreach (var item in serialsfromlocation)
// {
// db.UpdateSerialNumber(item);
// }
// return RedirectToAction(@"ScanIn");
// }
// else
// return View();
// }
// else
// return View();
//}
[HttpGet]
// GET: /Inventory/ScanOut
public ActionResult ScanOut()
{
return View();
}
[HttpPost]
//POST: /Inventory/ScanOut
public ActionResult ScanOut(string stringserial)
{
return View();
}
//METHODS:
//query for serials in location
public IEnumerable<SerialNumber> GetSerialsFromLocation(string stringLocation)
{
//to get the serials we need the location and a list of serials
//then we can iterate through the list and get only serials
//in the given location
var serialnumbers = db.GetSerialNumbers(new SerialNumberFilter(), new PagingOptions(0, 999, true));
var foundSerial = serialnumbers.Find(x => x.InventoryLocationName.Equals(stringLocation));
//query for serials in given location
var serialsinlocation =
from s in serialnumbers
where s.InventoryLocationName == stringLocation
select s;
return serialsinlocation;
//ViewBag.SerialsInLocation = serialsinlocation.ToString();
}
public static object MapObjects(object source, object target)
{
foreach (PropertyInfo sourceProp in source.GetType().GetProperties())
{
var targetProp = target.GetType().GetProperties().Where(p => p.Name == sourceProp.Name).FirstOrDefault();
if (targetProp != null && targetProp.GetType().Name == sourceProp.GetType().Name)
{
targetProp.SetValue(target, sourceProp.GetValue(source));
}
}
return target;
}
}
public class MappedEntity
{
}
}