VS 2013 Professional을 사용하여 MVC의 하위 뷰에서 기본 뷰로 데이터를 전달하려고 노력하고 있습니다. C #과 Razor를 주요 언어로 사용하고 있습니다. 내 하위보기에서 기본보기로 정보를 동적으로 게시하려고하는데 어떻게해야할지 모르겠습니다. 지금은 JavaScript를 사용하여 데이터를 다음과 같이 게시하고 있습니다.
#partialViewList#
@using WebApplication2.Models
@model IEnumerable<WebApplication2.Models.OBJECT>
<script>
function postItO(OfficeLocation, Name, Email, Phone, NumComputers, NumMonitors) {
form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', 'CompAndMon');
myvar = document.createElement('input');
myvar.setAttribute('OfficeName', "Primary Contact Name " + Name);
myvar.setAttribute('type', 'Office');
myvar.setAttribute('Email', "Primary Contact Email: " + Email);
myvar.setAttribute('Phone', "Primary Contact Phone: " + Phone);
myvar.setAttribute('NumComps', "Number of Computers: " + NumComputers);
myvar.setAttribute('NumMons', "Number Of Monitors: " + NumMonitors);
form.appendChild(myvar);
document.body.appendChild(form);
form.submit();
}
//I use the same general Idea when posting the other two models but so the code is not too long I will exclude them
</script>
<div class="panel-body col-lg-11 col-lg-offset-1" style="overflow-y: scroll;">
@foreach (var item in Model)
{
if (@item.Type == 1)
{
var office = item as Office;
string loc = @office.OfficeName;
string Name = @office.OfficeNumber;
string email = @office.ContactName;
string phone = @office.ContactNumber;
// int mons = @office.NumMonitors;
//int comps = @office.NumComputers;
<p><a onclick="postItO('@loc','@Name','@email','@phone')">@office.OfficeName</a></p>
}
else if (@item.Type == 2)
{
var computer = item as Computer;
string lst = @computer.LastUser;
string Nme = @computer.Name;
string TtHD = @computer.TotalHDSpace;
int NmUp = @computer.NumUpdates;
int NmMn = @computer.NumMonitors;
string FrHD = @computer.FreeHDSpace;
<p>  <a onclick="postItC('@lst','@Nme', '@TtHD','@FrHD','@NmMn','@NmUp')">@computer.Name</a></p>
}
else
{
var monitor = item as Monitor;
string man = @monitor.Manufacturer;
string mid = @monitor.ModelID;
string SN = @monitor.SerialNum;
int hr = @monitor.HoursON;
string TTi = @monitor.LastTestTime;
string TTy = @monitor.LastTestType;
<p>    <a onclick="postItM('@man', '@SN','@hr','@mid','@TTi','@TTy')">@monitor.Manufacturer</a></p>
}
}
</div>
CompAndMon이라는 메인 뷰에 게시하고 있습니다. 두 컨트롤러는 이렇게 보이지만 "익숙하지 않은"방식으로 정보를 게시하기 때문에 매개 변수를 추가해야하는지 확실하지 않습니다.
#Home Controller#
public ActionResult CompAndMon()
//I think I should put parameters in here but i am not sure
{
return View();
}
public ActionResult _OCMList()
{
var ObjectList = new List<OBJECT>{
new Office() {Type = 1,ID = 1, Name1 = "Fort Collins", OfficeName = "Fort Collins", OfficeNumber = "1", ContactNumber = "555-123-5555", ContactName = "ted" } ,
new Computer() {Type = 2,ID = 2,Name1 = "Speed-Machine", Name = "Speed-Machine", LastUser = "Ted", NumMonitors = 1, FreeHDSpace = "12GB", NumUpdates = 0, TotalHDSpace = "50GB" } ,
new Monitor() {Type = 3, ID = 3,Name1 = "Sony", Manufacturer = "Sony", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "a36-f45-gh325"} ,
new Office(){Type = 1,ID = 2, Name1 = "Denver", OfficeName = "Denver", OfficeNumber = "2", ContactNumber = "555-123-5555", ContactName = "Nick" } ,
new Computer() {Type = 2, ID = 5,Name1 = "Nicks PC", Name = "Nicks PC", LastUser = "Ted", NumMonitors = 1, FreeHDSpace = "12GB", NumUpdates = 0, TotalHDSpace = "50GB" } ,
new Monitor() {Type = 3, ID = 6,Name1 = "LG", Manufacturer = "LG", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "a38-l87kp-g6j9"} ,
new Computer() {Type = 2, ID = 7,Name1 = "Ted", Name = "FastOne", LastUser = "Ted", NumMonitors = 2, FreeHDSpace = "23GB", NumUpdates = 2, TotalHDSpace = "50GB" } ,
new Monitor() {Type = 3, ID = 8,Name1 = "HTC", Manufacturer = "HTC", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "d77-ko9-poo77" },
new Monitor() {Type = 3, ID = 9,Name1 = "Panisonic", Manufacturer = "Panisonic",HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "h67-j567-lo99" }
};
return PartialView(ObjectList);
}
내 메인 뷰에서 중요한 것은
#CompAndMon
///some javaScript or AJAX to grab posted values
<div class="container" id="MyPartial">
@Html.Action("_OCMList","Home")
</div>
나는 MVC를 처음 사용하기 때문에 이것이 많이 묻는다면 사과드립니다. 그래서 질문을 마무리하기 위해 ... 어떻게 내 메인 뷰에서 게시 된 변수를 얻을 수 있습니까?
PostModel 매개 변수를 취하고 GET과 동일한 이름을 갖는 새 조치 메소드에 값을 게시하여이를 수행 할 수 있습니다. 속성을 사용하여 둘을 구별 할 수 있습니다.
[HttpGet]
public ActionResult CompAndMon()
{
var viewModel = new ViewModel();
return View(viewModel);
}
[HttpPost]
public ActionResult CompAndMon(PostModel model)
{
var viewModel = new ViewModel
{
Thing = model.Thing
};
return View(viewModel)
}
public class PostModel
{
public object Thing { get; set; }
}
public class ViewModel
{
public object Thing {get; set; }
}
그런 다음 "기본"보기에서이 값이 null인지 확인하고 다시 게시 된 값이있는 경우 디스플레이를 채울 수 있습니다.
@model Project.Models.ViewModel
<div class="container">
@if (Model.Thing != null)
{
if (thing is Office office)
{
<!--Display Office-->
}
else if (thing is Computer computer)
{
<!--Display Office-->
}
else if (thing is Monitor monitor)
{
<!--Display Office-->
}
}
@Html.Action("_OCMList", "Home");
</div>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다