![](/img/trans.png)
[英]Render a sub-view in a parent when the sub-view fetched JSON data from service in Backbone
[英]How to get POSTed data in MVC from sub-View to Main View?
我正在嘗試使用 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; }
}
然后在您的“主”視圖中,您可以檢查該值是否為空,並在有回發的值時填充顯示。
@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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.