Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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# 部分显示未使用BeginCollectionItem返回索引_C#_Asp.net Mvc - Fatal编程技术网

C# 部分显示未使用BeginCollectionItem返回索引

C# 部分显示未使用BeginCollectionItem返回索引,c#,asp.net-mvc,C#,Asp.net Mvc,更新代码-如果我使用按钮,则AJAX调用不起作用,在chrome调试中从不点击,如果我使用操作链接尝试将它自己发送给我的部分视图添加到部分视图,而不是在预期的索引内 而不是 @Html.ActionLink(“Add”,“RequestedIpManager”,null,new{id=“addItem”}) 尝试使用在线指南中更复杂的模型结构将BeginCollectionItem付诸实践 我已经撤销了add item按钮的行为-它在转到分部后不会返回到索引 所以我只是在局部视图中结束,我处

更新代码-如果我使用按钮,则AJAX调用不起作用,在chrome调试中从不点击,如果我使用操作链接尝试将它自己发送给我的部分视图添加到部分视图,而不是在预期的索引内


而不是

@Html.ActionLink(“Add”,“RequestedIpManager”,null,new{id=“addItem”})

尝试使用在线指南中更复杂的模型结构将BeginCollectionItem付诸实践

我已经撤销了add item按钮的行为-它在转到分部后不会返回到索引

所以我只是在局部视图中结束,我处理代码的方式和我一样

我怎样才能让它返回到索引?唯一的区别是索引模型不是IEnumerable,但对象是,我是不是用错了

代码:

索引

@model Project.Models.Pa_Ipv4
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="ui-block-a">
                    <p>Company Name</p>
                    <span>
                        @Html.EditorFor(m => m.companyDetails.name)
                        @Html.ValidationMessageFor(m => m.companyDetails.name)
                    </span>
                </div>
@using (Html.BeginForm())
            {
                <div id="editorRows">
                    @foreach (var item in Model.requestedIps)
                    {
                        @Html.Partial("ReqIpView", item)
                    }
                </div>
                <div data-role="controlgroup" data-type="horizontal">
                    <input type="button" id="addItem" value="Add" />
                </div>
            }
            <br />
@section scripts
{
    <script type="text/javascript">
            $("#addItem").click(function() {
                $.ajax({
                    url: @Url.Action("RequestedIpManager"),
                    cache: false,
                    success: function(html) { $("#editorRows").append(html); }
                });
                return false;
            });
            $('#editorRows').on('click', '.deleteRow', function() {
                $(this).closest('.editorRow').remove();
            });
    </script>
}
@model Project.Models.Pa\u Ipv4
@{
Layout=“~/Views/Shared/_Layout.cshtml”;
}
公司名称

@EditorFor(m=>m.companyDetails.name) @Html.ValidationMessageFor(m=>m.companyDetails.name) @使用(Html.BeginForm()) { @foreach(Model.requestedIps中的var项) { @Html.Partial(“RequipView”,项目) } }
@节脚本 { $(“#添加项”)。单击(函数(){ $.ajax({ url:@url.Action(“RequestedIpManager”), cache:false, 成功:函数(html){$(“#editorRows”).append(html);} }); 返回false; }); $('#editorRows')。在('单击','.deleteRow',函数()上{ $(this).closest('.editorRow').remove(); }); }
部分

    @model Project.Models.IpAllocation
@using HierarchicalControlsDemo.Helpers
<div class="editorRow">
    @using (Html.BeginCollectionItem("requestedIps"))
    {
        <div class="ui-grid-c ui-responsive">
            <div class="ui-block-a">
                <span>
                    @Html.TextBoxFor(m => m.subnet)
                </span>
            </div>
            <div class="ui-block-b">
                <span>
                    @Html.TextBoxFor(m => m.cidr)
                </span>
            </div>
            <div class="ui-block-c">
                <span>
                    @Html.TextBoxFor(m => m.mask)
                </span>
            </div>
            <div class="ui-block-d">
                <span>
                    <a href="#" class="deleteRow">Dlt</a>
                </span>
            </div>
        </div>
    }
</div>
@model Project.Models.IpAllocation
@使用HierarchycalControlsDemo.Helpers
@使用(Html.BeginCollectionItem(“requestedIps”))
{
@Html.TextBoxFor(m=>m.subnet)
@Html.TextBoxFor(m=>m.cidr)
@Html.TextBoxFor(m=>m.mask)
}
控制器

public ActionResult Index()
        {
            Pa_Ipv4 paipv4 = new Pa_Ipv4
            {
                requestedIps = new List<IpAllocation>
                {
                    new IpAllocation {cidr = "", mask = "", subnet = "", allocationType = "requestedIp"}
                },
                existingIps = new List<IpAllocation>
                {
                    new IpAllocation {cidr = "", mask = "", subnet = "", allocationType = "existingIp"}
                }
            };
            return View(paipv4);
        }
public ViewResult RequestedIpManager()
        {
            return View("ReqIpView", new IpAllocation());
        }
public ActionResult Index()
{
Pa_Ipv4 paipv4=新的Pa_Ipv4
{
requestedIps=新列表
{
新IpAllocation{cidr=”,mask=”,subnet=”,allocationType=“requestedIp”}
},
existingIps=新列表
{
新IpAllocation{cidr=”,mask=”,subnet=”,allocationType=“existingIp”}
}
};
返回视图(paipv4);
}
public ViewResult RequestedIpManager()
{
返回视图(“RequipView”,new IpAllocation());
}
型号-为清晰起见

    public class Pa_Ipv4
    {
        public Details companyDetails { get; set; } // just a basic holding class for company input
        public List<IpAllocation> requestedIps { get; set; }
    }
public class IpAllocation
    {
        [Key]
        public int id { get; set; }
        [Required]
        public string allocationType { get; set; }
        [Required]
        public string subnet { get; set; }
        [Required]
        public string cidr { get; set; }
        [Required]
        public string mask { get; set; }
    }
公共类Pa_Ipv4
{
public Details companyDetails{get;set;}//只是公司输入的一个基本保持类
公共列表请求IP{get;set;}
}
公共类IpAllocation
{
[关键]
公共int id{get;set;}
[必需]
公共字符串分配类型{get;set;}
[必需]
公共字符串子网{get;set;}
[必需]
公共字符串cidr{get;set;}
[必需]
公共字符串掩码{get;set;}
}

这有一些小问题

ajax无法正常工作,因为
@url.action(actionName)
没有在浏览器上返回无效的字符串值-检查chrome调试器

编辑-更改为最佳实践URL

为避免出现这种情况,您的分区脚本应如下所示:

$(函数(){
$('#addItem')。在('单击',函数()上){
$.ajax({
url:'url.Action(“RequestedIpManager”),
cache:false,
成功:函数(html){$(“#editorRows”).append(html);}
});
返回false;
});
});

然后在控制器中,在控制器返回完整视图时进行以下更改,因此当单击实际起作用时,您将在其中获得整个页面:

public ActionResult RequestedIpManager()
{
返回PartialView(“RequipView”,new IpAllocation());

}

您为类型的
请求的
生成分部,但分部具有
@model Project.Models.ClassA
(假设是打字错误?),并且您使用
BeginCollectionItem(“requesteds”)
,但属性名称是
请求的
(不是复数),因此也不起作用。ajax调用的是名为
RequestedManager()
的方法,但显示的是
RequestedIpManager
。你需要发布你真正的代码!抱歉,我做了更改-这是我试图改进的一个项目的代码,以使其动态(并实际工作),但在我将其转移到实时项目之前,它是一个演示/测试项目。因此,您是否声称当您单击“添加”按钮时,您实际上重定向到仅显示部分内容的视图(而不是停留在同一页面上)?是的,这就是正在发生的事情。我用的是你在提及中为我修改的代码