简体   繁体   English

编辑项目C#的列表

[英]edit list of item c#

I wanna update a list of item in edit page, a user pass to edit page and update request for a list of question, a model for request is ready to use, edit.cshtml is like 我想更新编辑页面中的项目列表,用户通过编辑页面并更新对问题列表的请求,可以使用请求模型, edit.cshtml就像

@using (Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
    @foreach (var item in Model.Requesttables)
    {
        <div class="editor-label">
            @Html.LabelFor(modelItem => item.request)
        </div>
        <div class="editor-field">
            @Html.EditorFor(modelItem => item.request)
            @Html.ValidationMessageFor(modelItem => item.request)
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    }
</fieldset>
}

how the controler will be?? 控制器将如何?

public ActionResult Edit(List <Requesttable> requestlist)
    {//some logic here!}

If I understand it right, you want to see the controller. 如果我理解正确,则希望看到控制器。 First I think that there is something wrong with your controller signature. 首先,我认为您的控制器签名有问题。 It should be like this: 应该是这样的:

public ActionResult Edit(int id)
{
//search the object, no matter what it is - as long as it is form database by id
var db = new DbContext();
var yourRequestedList=db.Find(id); //or something like that, see linq for the correct sintax

yourRequestedList = objectThatWasEdited; 
}

I hope this helps you out, and don't forget to refactor. 希望这对您有所帮助,并且不要忘记重构。 My cod is not a good practice, you do not instatiate the db inside of a controller method. 我的鳕鱼不是一个好习惯,您不要在控制器方法中实例化数据库。

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

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