MCV5 My Razor模型类型为:IOrderedEnumerable
我无法弄清楚如何回发到控制器。如果我使用常规的ViewModel类,它工作正常,但因为我的Razor页面上有一个Header / Detail, 当我尝试回发时,作为IOrderedEnumerable,一个错误立即生成,我无法弄清楚如何回发...
我的ViewModel:
public class DocumentViewModel
{
[Display(Name = "Repostitory")]
public string Repository { get; set; }
public int DocId { get; set; }
[Display(Name = "Statement Date")]
public DateTime DocDate { get; set; }
[Display(Name = "Account Number")]
public string AcctNum {get;set;}
public string FND { get; set; }
public string strURL { get; set; }
public int UserID { get; set; }
}
我的代码用于填充视图模型的标题/详细信息:
{
ViewBag.dropdownMonths = new SelectList(db.vDDLMonths.ToList(), "codeID", "codeText","0");
ViewBag.dropdownYears = new SelectList(db.vDDLYears.ToList(), "codeID", "codeText","2016");
ViewBag.UserId = Convert.ToString(model.userID);
//Generate Querystring
//Pull Account List
string strQSAccountList = (from u in db.vUserParticipantQS
where u.userID == model.userID
select u.AccountNumbersList).FirstOrDefault();
string strQueryString = BuildQueryString("List");
string strMonth = "0";
string strYear = "2016";
string strStartDate = strYear;
string strEndDate = strYear;
int intMonth = 0;
if (int.TryParse(strMonth, out intMonth))
{
if (intMonth > 0)
{
strStartDate += strMonth;
strEndDate = strStartDate;
}
else
{
strStartDate += "01";
strEndDate += "12";
}
}
//Pull Months based on
strQueryString = strQueryString + "&BYM=" + strStartDate + "&EYM=" + strEndDate + "&A=" + strQSAccountList;
string newURL = BuildEncryptedURLRequest(strQueryString);
List<DocumentViewModel> docs = new List<DocumentViewModel>();
docs = XMLBuild.CreateXML(newURL, model.userID);
//Group results (for display in Razor view)
var queryAcct =
from doc in docs
group doc by doc.AcctNum into AcctGroup
orderby AcctGroup.Key
select AcctGroup;
return View("~/Views/Statements/Docs.cshtml", queryAcct);// This passes a type of IOrderedEnumerable<IGrouping<string, AodNetIntegration.Models.DocumentViewModel>>
}
public static List<DocumentViewModel> CreateXML(string newURL, int UserID)
{
XmlDocument docXML = new XmlDocument();
docXML.Load(newURL);
XmlNodeList nodes = docXML.DocumentElement.SelectNodes("/page/ALCHEMY/DOC");
List<DocumentViewModel> docs = new List<DocumentViewModel>();
int Id = 0;
DateTime dt;
string qs = "";
string url = "";
string docDate="";
foreach (XmlNode node in nodes)
{
DocumentViewModel doc = new DocumentViewModel();
doc.Repository = node.SelectSingleNode("REPOSITORY").InnerText;
if (int.TryParse(node.SelectSingleNode("DOC_ID").InnerText, out Id))
doc.DocId = Id;
if (DateTime.TryParse(node.SelectSingleNode("DOC_DATE").InnerText, out dt))
{
doc.DocDate = dt;
docDate = dt.ToString("MM/dd/yyyy");
}
doc.AcctNum = node.SelectSingleNode("A").InnerText; //node.Attributes["id"].Value;
doc.FND = node.SelectSingleNode("FND").InnerText;
doc.UserID = UserID;
//create direct link for each statement
qs = BuildQueryString("Document") + "&A=" + doc.AcctNum + "&DTE=" + docDate; //A=ACG473&DTE=07/31/2016;
url = BuildEncryptedURLRequest(qs);
doc.strURL = url;
docs.Add(doc);
}
Debug.WriteLine("Total docs: " + docs.Count);
docs.OrderBy(o => o.AcctNum)
.ThenBy(o => o.DocDate);
List<DocumentViewModel> docAccts = new List<DocumentViewModel>();
return docs;
}
型号代码:
@model IOrderedEnumerable<IGrouping<string, AodNetIntegration.Models.DocumentViewModel>>
@using (Html.BeginForm("GetStatement", "Statements", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.DropDownList("dropdownMonths",null, new { style = "width: 200px;" })
@Html.DropDownList("dropdownYears", null, new { style = "width: 200px;" })
<input type="submit" value="GO" class="btn btn-default" /> @*Post "GO" button that does not work!!*@
<div id="detailsDiv">
@foreach (var AccountGroup in Model )
{
<p><h1>@AccountGroup.Key</h1></p>
foreach (var item in AccountGroup)
{
<p>
@*@Html.ActionLink(@item.DocDate.ToShortDateString(), "GetStatement", "Statements", new {id= @ViewBag.UserID, statementDate = @item.DocDate.ToShortDateString(), acctNumber = AccountGroup.Key.ToString() }, new { @class = "form-control, col-sm-6, medwidth" })
@Html.ActionLink("View", "ViewPDF", new { target = @item.strURL })*@
<a href="@item.strURL.ToString()" target="_blank">@item.DocDate.ToString("MMM yyyy") Statement</a>
</p>
}
}
</div>
我的“Statements / GetStatement”控制器(从此视图返回) - 这不会被识别!!
public ActionResult GetStatement(IOrderedEnumerable<IGrouping<string, AodNetIntegration.Models.DocumentViewModel>> model, string returnUrl)
{//DO Stuff
}
我的GO按钮不会调用Controller方法,除非我将其更改为常规模型...有没有办法返回&gt;通过发布页面到控制器?如果没有,那么最好的方法是什么。我想通过AJAX这样做,所以我可以做一个部分的帖子,但似乎我必须首先找出帖子。我看过this和this,但不能弄清楚我错过了什么!谢谢!