简体   繁体   English

ASP.NET Core MVC-在同一视图中获取数据和处理404

[英]ASP.NET Core MVC - Getting data and handling 404 in same view

I'm more familiar with WebForms (which my company uses), and just started learning MVC. 我对WebForms(我的公司使用)更加熟悉,并且刚刚开始学习MVC。 I followed the movies tutorial, and am having a bit of a hard time seeing how I can use the MVC pattern the way I want. 我遵循了电影教程,并且很难看清如何以自己的方式使用MVC模式。

For instance, my company often asks me to make web apps that take some input from the user, looks it up in our database, and gives them back some other piece of data pertaining to the input, all on the same page. 例如,我的公司经常要求我制作Web应用程序,以从用户那里获取一些输入,在我们的数据库中查找它们,然后将与输入有关的其他一些数据返回给它们,它们都在同一页面上。

With WebForms I can easily see if the database returned null and display an error label in that case. 使用WebForms,我可以轻松查看数据库是否返回null并在这种情况下显示错误标签。 But with MVC treating data access like an API, I don't see how I can do the same thing without ignoring using the proper 404 code. 但是随着MVC像API一样对待数据访问,我看不出如何做同样的事情而不忽略使用正确的404代码。 Let's say I want to look up a movie (I commented out the 404s on purpose for now): 假设我要看一部电影(我现在故意注释掉了404s):

public class MoviesController : Controller
 {
    private readonly MovieDBContext _context;

    public MoviesController(MovieDBContext context)
    {
        _context = context;
    }

    // GET: Movies/Info/Title
    public async Task<IActionResult> Info(string title)
    {
        //if (title == null)
        //{
            //return NotFound();
        //}

        var movie = await _context.Movies
            .FirstOrDefaultAsync(m => m.Title == title);
       // if (movie == null)
       // {
           // return NotFound();
        //}

        return Json(movie);
   }

Then in the view: 然后在视图中:

<h2>App</h2>
<br />
<div class="form-group">
    <p>
        Price Lookup App
    </p>
    <br />
    <div class="row">
        <div class="col-xs-2">
            Enter movie title:
        </div>
        <div class="col-xs-2">
            <input type="text" class="form-control" id="title" />
        </div>
        <div class="col-xs-2">
            <input type="submit" class="btn" onclick="getMovie()" />
        </div>
    </div>
    <br />
    <div class="row">
        <label id="price"></label>
    </div>
</div>

<script>
    function getMovie() {
        var movie = $("#title").val();
        if (movie != "") {
            $.getJSON("../Movies/Info?title=" + movie, function (response) {
                if (response != null) {
                    $("#price").html(response.price);
                }
                else {
                    $("#price").html("Film not found");
                }
            });
        }
        else {
            $("#price").html("Film not found");
        }
    }
</script>

This technically works, but I feel I'm really not supposed to do it this way. 从技术上讲,这是可行的,但是我真的不应该这样做。 I should be giving a 404 code when the movie isn't found, correct? 找不到电影时,我应该提供404代码,对吗? I just don't know how to handle getting a 404 response and displaying a custom "not found" message on the same view like I am above, because the 404 stops execution of the JS. 我只是不知道如何处理获得404响应并像上面一样在同一视图上显示自定义“未找到”消息,因为404会停止执行JS。 Thanks. 谢谢。

You can use $.ajax (ajax call), or $.get(get api call). 您可以使用$ .ajax(ajax调用)或$ .get(获取api调用)。 There are componenets of success and error. 有成功和错误的组成部分。 you can handle the responses there 您可以在那里处理回应

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

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