簡體   English   中英

在導航上傳遞模型是空的

[英]Passing Model on navigation is Null

我有一個MVC項目,我試圖在導航時將serializedForm(構建LRSSearchMaster模型)傳遞給我的控制器。 因此,如前所述,我有一個名為LRSSearchMaster的模型。 當我調用頁面更改時,它會成功傳遞所有參數,但LRSSearchMaster除外。 所有值都為null(即Party為null),即使它不應為null。 在導航時,如何將此模型與一些額外參數傳遞給模型?

模型

public partial class LRSSearchMaster
{
    public LRS_Party Party { get; set; }
    public LRS_Settings Settings{ get; set; }
    public LRS_IndexedInstrument IndexedInstrument { get; set; }
    public LRS_InstrumentSubType InstrumentSubType { get; set; }

    [UIHint("DateFilter")]
    [DisplayName("Date Filter")]
    public int dateFilterValue { get; set; } = 0;

    public LRS_BookTypes BookTypes { get; set; }

    public DateTime fromDateFile { get; set; }
    public DateTime toDateFile { get; set; }
    public String ReverseSearchName { get; set; }

    public int grp { get; set; }

    public bool selectFile { get; set; } = false;
}

HTML

<a href='@Url.Action("SearchFilter1", "SearchFilter")' onclick='navigate(this.href);'>
    @*<input type="button" value='Submit' />*@
    <input id="btnSearch" type="button" value='Search (F8)' />
</a>

阿賈克斯

$.ajax({
    url: "@Url.Action("SearchByNameLookUp", "SearchByName")",
    data: JSON.stringify({ oSearchByName: oModel }),
    type:"POST",
    success: function (data)
    {
        if (data.succeed != true)
        {
            alert(data.errors);
            bContinue = false;
        }
        else
        {
            oModel = data.oModel;
            url = target + "?SM=" + JSON.stringify(data.oModel) + "&searchType=" + 1;
            window.location.href = url
        }
    },
    error: function (data)
    {
        alert("Error creating/loading Case. Please refresh the page and try again.");
    }
});

調節器

public ActionResult SearchFilter(LRSSearchMaster model, int searchType = 0)
{
    List<LRSSearchMaster> liSM = GetFilteredResults(model, searchType);
    ViewBag.searchType = searchType;
    return View("~/Areas/LRSSearch/Views/SearchFilter/SearchFilter.cshtml", liSM);
}

我不確定你的代碼中哪個“oModel”變量來自你的Javascript環境。 假設oModel實際上包含它應該包含的內容,我懷疑Javascript中的AJAX參數名稱應該是model ,或者你的控制器變量應該是oSearchByName

但是,在編寫API2時我只使用過JSON作為參數,所以如果你不能使JSON正常工作,你可能最好為模型中的每個字段傳遞一個Javascript字段,如:

{ model_Var1: 'a', model_Var2: 'b', ... }

所以我最近做的就是將我的模型轉換為json字符串並將其發送給我的控制器。 雖然我的參數仍為null,但我可以通過執行Request.QueryString [MyParameterName]來訪問該字符串

url = '@Url.Action("SearchFilter", "SearchFilter")?model=' + JSON.stringify(data.oModel) + "&searchType=" + 1; window.location.href = url;

然后在我的控制器中我做了以下。

model = JsonConvert.DeserializeObject<LRSSearchMaster>(Request.QueryString["model"]);

瓦哈拉! 我現在擁有通過的一切。

我會使用表單提交,但客戶希望能夠將URL發送給某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM