简体   繁体   English

控制器没有返回PartialView?

[英]Controller not returning PartialView?

I have a controller which contains an if statement but it shouldn't matter if it hits the first or second block because they both return the same Type. 我有一个控制器,它包含一个if语句但是如果它遇到第一个或第二个块则无关紧要,因为它们都返回相同的Type。

[HttpPost]
public ActionResult Descriptions(string partsNo, Details searchValue)
{
    var response = partsNo != null ? _referenceRepository.GetParts(partsNo) : _referenceRepository.SearchValue(searchValue);

    return PartialView(response);
}

View 视图

@model ArrowEngineeringMVC.Models.Details

<script>
    $(document)
        .on("click",
            ".getPartNo",
            function (e) {
                var val = $(this).val();
                $.ajax({
                    url: '/Home/Descriptions',
                    contentType: 'application/json',
                    type: 'POST',
                    data: JSON.stringify({ "partsNo": val }),
                    dataType: 'html'
                })
                    .success(function (result) {
                        $('#descriptionPlace').html(result);
                    })
                    .error(function (xhr, status) {
                        alert(status);
                    });
            })
</script>

@using (Html.BeginForm("Descriptions", "Home", FormMethod.Post))
{
    <div class="topBar">
        @Html.TextBoxFor(m => m.Type)
        <input type="submit" id="submitId" value="submit"/>
    </div>
}

    <div class="leftPanel">
        <button href="javascript:void(0)" id="NumbersButton" value="Number" class="getPartNo">#</button>
        <button href="javascript:void(0)" value="A" class="getPartNo">A</button>
        <button href="javascript:void(0)" value="B" class="getPartNo">B</button>
        <button href="javascript:void(0)" value="C" class="getPartNo">C</button>
    </div>

    <div id="descriptionPlace"></div>

If you click one of the buttons it would hit the first part of the if statement. 如果单击其中一个按钮,它将触及if语句的第一部分。 If you do this then a PartialView is returned and everything works fine, the value from the button is being passed to the controller using AJAX whereas if you do a search it is being passed via the model this is the only difference. 如果你这样做,那么返回一个PartialView并且一切正常,按钮的值将使用AJAX传递给控制器​​,而如果你进行搜索,它将通过模型传递,这是唯一的区别。

If you do a search it hits the second part of the if statement which doesn't return a partial view it just returns a new view for some reason. 如果你进行搜索,它会触及if语句的第二部分而不返回部分视图,它只是出于某种原因返回一个新视图。

I'm just confused because the variable response returns a List but just contains different values depending on which part of the if statement you hit. 我只是感到困惑,因为变量响应返回一个List但只包含不同的值,具体取决于你命中的if语句的哪一部分。

Heres a bit of the referenceRepository just to make it easier to understand: 继承了一些referenceRepository只是为了让它更容易理解:

public List<Parts> SearchValue(Details searchValue)
{
    const string detailsSql = "SELECT [ID], [Description] FROM Table1 WHERE [Type] = {0}";
    string queryDetails = string.Format(detailsSql, searchValue.Type);

    var dbDetails = getConnection().Query<Parts>(queryDetails).ToList();

    return dbDetails;
}

public List<Parts> GetParts(string partNumber)
{
    string partsSql;
    if (partNumber == "Number")
    {
        partsSql = "SELECT [ID], [Description] FROM Table1 WHERE [Part Number]  not like '%[^0-9]%'";
    }
    else
    {
        partsSql = "SELECT [ID], [Description] FROM Table1 WHERE [Part Number] like '{0}%'";
    }

    string queryParts = string.Format(partsSql, partNumber);

    var dbParts = getConnection().Query<Parts>(queryParts).ToList();

    return dbParts;
}

if you want same result as partial view in #descriptionPlace div on submit click.then you can try like this remove form tag.and bind the click event for submit buttons along with those getnumbers buttons.. 如果你想在提交点击的#descriptionPlace div中得到与部分视图相同的结果。那么你可以尝试这样删除表格标签。并绑定提交按钮的点击事件以及那些getnumbers按钮..

Not require to post for one value? 不需要发布一个值? am i right? 我对吗?

if you want to specific about to load partial view in div or specific area then you have to mention like ajax call.or you can use @Html.Partial or RenderPartial.But your requirement little bit odd. 如果你想具体要在div或特定区域加载局部视图,那么你必须提到像ajax call.or你可以使用@ Html.Partial或RenderPartial.But你的要求有点奇怪。 So you can try like this.. 所以你可以尝试这样..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM