Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何从查询字符串中获取外键并将其添加到数据库的新记录中?_Asp.net_Asp.net Core - Fatal编程技术网

Asp.net 如何从查询字符串中获取外键并将其添加到数据库的新记录中?

Asp.net 如何从查询字符串中获取外键并将其添加到数据库的新记录中?,asp.net,asp.net-core,Asp.net,Asp.net Core,我对ASP Net核心非常陌生。我的项目将举行董事会。每个导演都有一个页面,显示他/她的电影列表 我有两节课。电影: public class Movie { public int MovieId { get; private set; } public int DirectorId { get; set; } [Required] public string Title { get; set; } public string Year { get; se

我对ASP Net核心非常陌生。我的项目将举行董事会。每个导演都有一个页面,显示他/她的电影列表

我有两节课。电影:

public class Movie
{
    public int MovieId { get; private set; }
    public int DirectorId { get; set; }
    [Required]
    public string Title { get; set; }
    public string Year { get; set; }
}
和主任:

public class Director
{
    public int DirectorId { get; private set; }
    [Required]
    public string Name { get; set; }
    public string Country { get; set; }
    public string Bio { get; set; }
    public List<Movie> Movies { get; set; }
}
公共类主管
{
public int DirectorId{get;private set;}
[必需]
公共字符串名称{get;set;}
公共字符串国家{get;set;}
公共字符串Bio{get;set;}
公共列表电影{get;set;}
}

我有一个显示董事名单的索引页。请看一下:

如你所见,我在每个导演旁边都放了一个“添加电影”按钮。点击它,你将导航到一个页面,你可以添加一部电影。但问题是我想把导演Id传给那个页面。Movie类有一个名为“DirectorId”的外键。每部新电影都必须在其“DirectorId”字段中自动添加导演Id

通过Index.cshtml中的这行代码,我已经在url中添加了DirectorId:

<a asp-page="./Movies/Create" asp-route-DirectorId="@item.DirectorId">Add Movie</a>


(为了更好地理解,请参见上图)

我根据您的代码和需求制作了一个示例

索引视图

结果:


希望它能帮助您。

我根据您的代码和需求做了一个示例

索引视图

结果:


希望它能帮助您。

那么问题出在哪里?看来你做得对。当创建页面将调用get方法时,您将在get方法参数中获取directorid。您必须在创建页面上有如下的get方法:public-IActionResult-OnGet(int-id)。@user13422309是的,我想我应该使用OnGet。但这是我的第一个项目。我已经试过了,但失败了。我会用你说的“public-IActionResult-OnGet(int-id)”再试一次。谢谢,有什么问题吗?看来你做得对。当创建页面将调用get方法时,您将在get方法参数中获取directorid。您必须在创建页面上有如下的get方法:public-IActionResult-OnGet(int-id)。@user13422309是的,我想我应该使用OnGet。但这是我的第一个项目。我已经试过了,但失败了。我会用你说的“public-IActionResult-OnGet(int-id)”再试一次。谢谢。要点是CreateModel code.@S.Serpooshan-Aye中的
OnGet(int-DirectorId)
。谢谢。要点是CreateModel code.@S.Serpooshan-Aye中的
OnGet(int-DirectorId)
。谢谢
<h1>Index</h1>

<p>
    <a asp-page="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Directors[0].Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Directors[0].Country)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Directors[0].Bio)
            </th>

            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Directors)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Country)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Bio)
                </td>
                <td>
                    <a asp-page="./Movies/Create" asp-route-DirectorId="@item.DirectorId">Add Movie</a>
                </td>
            </tr>
        }
    </tbody>
</table>
<h4>Movie</h4>
<hr />
<div class="row">
<div class="col-md-4">
    <form method="post">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Movie.DirectorId" class="control-label"></label>
            <input asp-for="Movie.DirectorId" class="form-control" />
            <span asp-validation-for="Movie.DirectorId" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Movie.Title" class="control-label"></label>
            <input asp-for="Movie.Title" class="form-control" />
            <span asp-validation-for="Movie.Title" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Movie.Year" class="control-label"></label>
            <input asp-for="Movie.Year" class="form-control" />
            <span asp-validation-for="Movie.Year" class="text-danger"></span>
        </div>

        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>
</div>
public class IndexModel : PageModel
{
    public IList<Director> Directors { get; set; }

    public void OnGet()
    {

        Directors = new List<Director>
        {
            new Director{ Name = "AA", Country = "C1", DirectorId = 1, Bio = "XXXXX" },
            new Director{ Name = "BB", Country = "C2", DirectorId = 2, Bio = "XXXXX" },
            new Director{ Name = "CC", Country = "C3", DirectorId = 3, Bio = "XXXXX" }
        };

    }
}
public class CreateModel : PageModel
{

    public Movie Movie { get; set; }

    public void OnGet(int DirectorId)
    {
        Movie = new Movie { DirectorId = DirectorId };
    }
}