将ViewData值从控制器传递到视图

时间:2017-08-03 09:31:24

标签: javascript c# jquery asp.net asp.net-mvc

我正在对我的控制器方法进行ajax调用,并希望将结果返回到我的视图中。但是,即使控制器返回SELECT * FROM `yourtable` WHERE (your_found_date BETWEEN 'start_date' AND 'end_date') 值,我的视图也不会显示来自控制器方法的值。

扫描仪视图(为简洁起见剥离版本)

ViewData

家庭控制器

<div class="card card-outline-danger card-danger">
    <div class="card-block">
        <blockquote class="card-blockquote">
            <p>Result In</p>
            <div id="resultsList"></div>
            <p>Result Out</p>
            @ViewData["TextAreaResult"]
        </blockquote>
    </div>
</div>

JS(简化版剥离版)

public IActionResult Scanner() {
    ViewData["TextAreaResult"] = "No Scan yet";
    return View();
}

[HttpPost]
public IActionResult DecodeScanner(string productCode, string serialNumber, string batch, string expirationDate, int commandStatusCode) {
    try {
        Control.Initialize();
        Control control = new Control();
        Request request = new Request();

        request.setProductCode(productCode);
        request.setSerialNumber(serialNumber);
        request.setBatch(batch);
        request.setExpirationDate(expirationDate);
        request.setCommandStatusCode(commandStatusCode);

        control.sendSinglePackOperation(request);

        Console.WriteLine("Response:" + request.getHttpResponseCode());

        ViewData["TextAreaResult"] = string.Format(" Response {0}", request.getHttpInformation());

    } catch (Exception exc) {
        ViewData["TextAreaResult"] = "Exception: " + exc.Message;
    }

    return View("Scanner");
}

在网络选项卡下的响应部分中,我可以看到控制器在成功调用ajax之后传递了结果,但更新后的$('#my-button').click(function() { $.ajax({ url: "DecodeScanner", type: "POST", data: { productCode: productCode, serialNumber: serialNumber, batch: batch, expirationDate: expirationDate, commandStatusCode: 0 }, async: true }); }); 值未在视图中呈现。我猜这是因为没有页面刷新而没有传递新值。

我期望的输出(在这种情况下)是显示@ViewData["TextAreaResult"]

网络标签 - Ajax通话响应 enter image description here

HTML视图 - 仍显示Response The pack is active函数中定义的默认@ViewData["TextAreaResult"]文本。 enter image description here

2 个答案:

答案 0 :(得分:1)

您无法更新已呈现的@ViewData["TextAreaResult"]。你需要这样做才能实现这个目标

扫描程序视图

将viewdata放入p标签

  <p id="pTextAreaResult">@ViewData["TextAreaResult"]</p>

<强>控制器

[HttpPost]
public IActionResult DecodeScanner(string productCode, string serialNumber, string batch, string expirationDate, int commandStatusCode) {
    string TextAreaResult = string.Empty;
    try {
        Control.Initialize();
        Control control = new Control();
        Request request = new Request();

        request.setProductCode(productCode);
        request.setSerialNumber(serialNumber);
        request.setBatch(batch);
        request.setExpirationDate(expirationDate);
        request.setCommandStatusCode(commandStatusCode);

        control.sendSinglePackOperation(request);

        Console.WriteLine("Response:" + request.getHttpResponseCode());

        TextAreaResult = string.Format(" Response {0}", request.getHttpInformation());

    } catch (Exception exc) {
        TextAreaResult = "Exception: " + exc.Message;
    }

    return Json(TextAreaResult, JsonRequestBehavior.AllowGet);
}

<强>脚本

$('#my-button').click(function() {
    $.ajax({
        url: "DecodeScanner",
        type: "POST",
        data: {
            productCode: productCode,
            serialNumber: serialNumber,
            batch: batch,
            expirationDate: expirationDate,
            commandStatusCode: 0
        },
        async: true,
        success: function(data) {
           $('#pTextAreaResult').text(data);
        }
    });
});

答案 1 :(得分:0)

我无法在您的代码中看到您使用返回的HTML实际更新View的任何位置?

如果您在AJAX帖子中添加成功回调,则可以执行以下操作:

$('#my-button').click(function() {
    $.ajax({
        url: "DecodeScanner",
        type: "POST",
        data: {
            productCode: productCode,
            serialNumber: serialNumber,
            batch: batch,
            expirationDate: expirationDate,
            commandStatusCode: 0
        },
        async: true            
    }).done(function(html) {
        $('.card').replaceWith(html);
    });
});

(未经测试,但希望你能得到这个想法)