Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.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
C# 加载带有htmlbeginform的partialview的视图_C#_Asp.net Mvc - Fatal编程技术网

C# 加载带有htmlbeginform的partialview的视图

C# 加载带有htmlbeginform的partialview的视图,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个有很多组件的项目,这些组件中的任何一个都可以用更多的选项进行更新。因此,我认为使用一个下拉列表,允许您选择要添加选项的组件是最好的,然后它可以加载一个局部视图,其中包含要添加选项的特定组件的输入字段,然后按submit。但它似乎不能很好地处理HTML.BeginForm,我不确定为什么。更具体地说,我不知道这是因为它是一个局部视图,还是我把begin表单搞砸了 编辑:对不起,我没有解释运行时会发生什么。当我按下提交按钮时,什么也没有发生。只是什么都没发生,它不会发送到partialvie

我有一个有很多组件的项目,这些组件中的任何一个都可以用更多的选项进行更新。因此,我认为使用一个下拉列表,允许您选择要添加选项的组件是最好的,然后它可以加载一个局部视图,其中包含要添加选项的特定组件的输入字段,然后按submit。但它似乎不能很好地处理HTML.BeginForm,我不确定为什么。更具体地说,我不知道这是因为它是一个局部视图,还是我把begin表单搞砸了

编辑:对不起,我没有解释运行时会发生什么。当我按下提交按钮时,什么也没有发生。只是什么都没发生,它不会发送到partialview的post部分

以下是选择组件的页面:

<p></p>
<h3>What needs to be added?</h3>
<div id="match-alert" class="alert" role="alert"></div>
<select class="form-control" id="Type-Selector" name="Type-Selector">
        <option value="-1">Select Option</option>
        <option value="1">New Manufacturer</option>
        <option value="2">New Model<option>
        <option value="3">New Series</option>
</select>

<div id="partialPlaceHolder" style="display:none;"></div>

@section Scripts{
    <script src="http://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
           $("#Type-Selector").change(function () {
                var selectedID = $(this).val();

                $.get("/Components/SelectedItem/" + selectedID, function (data) {
                    $('#partialPlaceHolder').html(data);
                    $('#partialPlaceHolder').fadeIn('fast');

        })
    </script>
    }
它返回的部分视图:

@model AutoDealer.ViewModels.UpdateManufacturerViewModel

<table id="ManufactureEntry"  class="d-flex justify-content-center" style="width:20%">
    <thead>
        <tr>
            <th>New Manufacturer</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            @using (Html.BeginForm("_UpdateManufacturer","Components",  FormMethod.Post))
            {
                <td>@Html.TextBoxFor(Model => Model.Manufacturer.cDescription )</td>
                <td><input id="submit" type="submit" value="Add" /></td>
            }
        </tr>
    </tbody>
</table>
@model AutoDealer.ViewModels.UpdateManufacturerViewModel
新制造商
@使用(Html.BeginForm(“\u UpdateManufacturer”,“Components”,FormMethod.Post))
{
@Html.TextBoxFor(Model=>Model.Manufacturer.cddescription)
}

所以我找到了一个解决方案,但我不知道为什么会发生变化。我会把我做的改变贴出来。我已将Html.BeginForm移到表组件上方。以下是代码更改:

@model AutoDealer.ViewModels.UpdateManufacturerViewModel

@using (Html.BeginForm("_UpdateManufacturer","Components",  FormMethod.Post))
{
    <table id="ManufactureEntry"  class="d-flex justify-content-center" style="width:20%">
        <thead>
            <tr>
                <th>New Manufacturer</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                    <td>@Html.TextBoxFor(Model => Model.Manufacturer.cDescription )</td>
                    <td><input id="submit" type="submit" value="Add" /></td>
            </tr>
        </tbody>
    </table>
}
@model AutoDealer.ViewModels.UpdateManufacturerViewModel
@使用(Html.BeginForm(“\u UpdateManufacturer”,“Components”,FormMethod.Post))
{
新制造商
@Html.TextBoxFor(Model=>Model.Manufacturer.cddescription)
}

现在,当partialview加载到主选择页面时,submit按钮起作用。我不明白为什么这个补丁会这么做。如果有人猜测或理解为什么我真的想知道,因为当我自己加载部分视图时,旧代码起作用了。只有当partialview加载到另一个页面时,它才起作用

当你说它打得不好时,它到底在做什么?您在浏览器开发工具中看到了什么输出?抱歉,我没有描述。当我按下submit按钮时,什么也没有发生。如果您检查dev工具(或任何类似工具)的network选项卡中对SelectedItem的调用,是否返回错误。您是否已在该方法中逐步执行了代码,以确保您获得的是selectedID值,并且正确评估了该值?它将返回包含用于输入新制造商的字段的局部视图。因此,该字段显示,我可以在该字段中输入文本。但是,当我提交HTML.Beginform时,它不会将信息传递给/Components/\u updateManufacturer httppost。您正在将其发布到
\u updateManufacturer/Components
。重载的正确顺序是
BeginForm(“actionName”,“controllerName”,FormMethod)
,因此您的应该是
BeginForm(“\u updateManufacturer”,“Components”,FormMethod.Post)
,尽管我很惊讶您没有收到错误404。
@model AutoDealer.ViewModels.UpdateManufacturerViewModel

@using (Html.BeginForm("_UpdateManufacturer","Components",  FormMethod.Post))
{
    <table id="ManufactureEntry"  class="d-flex justify-content-center" style="width:20%">
        <thead>
            <tr>
                <th>New Manufacturer</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                    <td>@Html.TextBoxFor(Model => Model.Manufacturer.cDescription )</td>
                    <td><input id="submit" type="submit" value="Add" /></td>
            </tr>
        </tbody>
    </table>
}