Displaying, Editing, and Posting a List ASP.NET Core

huangapple go评论84阅读模式
英文:

Displaying, Editing, and Posting a List ASP.NET Core

问题

以下是您要翻译的内容:

"I was trying to follow this question, https://stackoverflow.com/questions/9306246/posting-to-a-listmodeltype-mvc3, but I was running into a roadblock.

I want to display the full contents of a table, allow editing to that table, then submit it back to controller to save edits.

Specifically, the error I'm getting here is on Model.ClonePosts.Count:

'IEnumerable' does not contain a definition for 'ClonePosts' and no accessible extension method 'ClonePosts' accepting a first argument of type 'IEnumerable' could be found

Aside from that immediate problem, I'm not sure that have the binding correct. Will changes made in the View get posted correctly?

Thank you in advance

Model:

public class ClonePost
{
[Key]
public int id { get; set; }
[Required]
public string PostNumber { get; set; }
[Required]
public string Type { get; set; }
[Required]
public int ItemNumber { get; set; }
public string ItemData { get; set; }
public int ItemSelected { get; set; }
}

View model:

public class ReviewCloneVM
{
public List ClonePosts { get; set; }
}

View:

@model IEnumerable<AURA.ViewModels.ReviewCloneVM>

ReviewClone

@*foreach (var item in Model)*@
@for (int i = 0; i < Model.ClonePosts.Count; i++) {

}

@Html.EditorFor(x => x.ClonePosts[i].PostNumber) @Html.EditorFor(x => x.ClonePosts[i].ItemNumber) @Html.EditorFor(x => x.ClonePosts[i].ItemData) @Html.EditorFor(x => x.ClonePosts[i].ItemSelected)

Controller:

public async Task ReviewClone(string zero = "230323-0")
{
...
// now call up the full table for review and edit
var cloned = _context.ClonePosts.OrderByDescending(m => m.PostNumber);

return View(await cloned.ToListAsync());

}

[HttpPost]
public async Task ReviewClone(ReviewCloneVM reviewCloneVM)
{
try
{
// other stuff
return RedirectToAction("PostDetail", "Post", new { zero = "230323-0" });
}
catch
{
return View(reviewCloneVM);
}
}"

英文:

I was trying to follow this question, https://stackoverflow.com/questions/9306246/posting-to-a-listmodeltype-mvc3, but I was running into a roadblock.

I want to display the full contents of a table, allow editing to that table, then submit it back to controller to save edits.

Specifically, the error I'm getting here is on Model.ClonePosts.Count:

> 'IEnumerable<ReviewCloneVM>' does not contain a definition for 'ClonePosts' and no accessible extension method 'ClonePosts' accepting a first argument of type 'IEnumerable<ReviewCloneVM>' could be found

Aside from that immediate problem, I'm not sure that have the binding correct. Will changes made in the View get posted correctly?

Thank you in advance

Model:

public class ClonePost
{
    [Key]
    public int id { get; set; }
    [Required]
    public string PostNumber { get; set; }
    [Required]
    public string Type { get; set; }
    [Required]
    public int ItemNumber { get; set; }
    public string ItemData { get; set; }
    public int ItemSelected { get; set; }
}

View model:

public class ReviewCloneVM
{
    public List&lt;ClonePost&gt; ClonePosts { get; set; }
}

View:

@model IEnumerable&lt;AURA.ViewModels.ReviewCloneVM&gt;

&lt;h1&gt;ReviewClone&lt;/h1&gt;
&lt;table class=&quot;table&quot;&gt;
    @*foreach (var item in Model)*@
@for (int i = 0; i &lt; Model.ClonePosts.Count; i++) {
    &lt;tr&gt;
        &lt;td&gt;
            @Html.EditorFor(x =&gt; x.ClonePosts[i].PostNumber)
        &lt;/td&gt;
        &lt;td&gt;
            @Html.EditorFor(x =&gt; x.ClonePosts[i].ItemNumber)
        &lt;/td&gt;
        &lt;td&gt;
            @Html.EditorFor(x =&gt; x.ClonePosts[i].ItemData)
        &lt;/td&gt;
        &lt;td&gt;
            @Html.EditorFor(x =&gt; x.ClonePosts[i].ItemSelected)
        &lt;/td&gt;
        &lt;td&gt;
            &lt;input type=&quot;submit&quot; value=&quot;Clone&quot; /&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
}
&lt;/tbody&gt;
&lt;/table&gt;

Controller:

    public async Task&lt;IActionResult&gt;ReviewClone(string zero = &quot;230323-0&quot;)
    {
        ...
        // now call up the full table for review and edit
        var cloned = _context.ClonePosts.OrderByDescending(m =&gt; m.PostNumber);

        return View(await cloned.ToListAsync());
    }

    [HttpPost]
    public async Task&lt;IActionResult&gt;ReviewClone(ReviewCloneVM reviewCloneVM)
    {
        try
        {
            // other stuff
            return RedirectToAction(&quot;PostDetail&quot;, &quot;Post&quot;, new { zero = &quot;230323-0&quot; });
        }
        catch
        {
            return View(reviewCloneVM);
        }
    }

答案1

得分: 1

你需要将你的Model类型从@model IEnumerable&lt;AURA.ViewModels.ReviewCloneVM&gt; 更改为 AURA.ViewModels.ReviewCloneVM,然后从Model中访问ClonePosts

@model AURA.ViewModels.ReviewCloneVM

<h1>ReviewClone</h1>
<table class="table">
  @if(Model != null)
  {
      @*foreach (var item in Model)*@
      for (int i = 0; i &lt; Model.ClonePosts.Count; i++) 
      {
        <tr>
          <td>
            @Html.EditorFor(x => x.ClonePosts[i].PostNumber)
          </td>
          <td>
            @Html.EditorFor(x => x.ClonePosts[i].ItemNumber)
          </td>
          <td>
            @Html.EditorFor(x => x.ClonePosts[i].ItemData)
          </td>
          <td>
            @Html.EditorFor(x => x.ClonePosts[i].ItemSelected)
          </td>
          <td>
            <input type="submit" value="Clone" />
          </td>
        </tr>
      }	  
  }   
</tbody>
</table>

你的Controller方法将是:

public async Task<IActionResult> ReviewClone(string zero = "230323-0")
{
    // ...
    ReviewCloneVM model = new ReviewCloneVM();
    // 现在调用完整的表以供审查和编辑
    var cloned = _context.ClonePosts.OrderByDescending(m => m.PostNumber);
    model.ClonePosts = await cloned.ToListAsync();
    return View(model);
}
英文:

You need to change your Model type from @model IEnumerable&lt;AURA.ViewModels.ReviewCloneVM&gt; to AURA.ViewModels.ReviewCloneVM and then access the ClonePosts from the Model :

@model AURA.ViewModels.ReviewCloneVM

&lt;h1&gt;ReviewClone&lt;/h1&gt;
&lt;table class=&quot;table&quot;&gt;
  @if(Model !=null)
  {
	  @*foreach (var item in Model)*@
	  for (int i = 0; i &lt; Model.ClonePosts.Count; i++) 
	  {
		&lt;tr&gt;
			&lt;td&gt;
				@Html.EditorFor(x =&gt; x.ClonePosts[i].PostNumber)
			&lt;/td&gt;
			&lt;td&gt;
				@Html.EditorFor(x =&gt; x.ClonePosts[i].ItemNumber)
			&lt;/td&gt;
			&lt;td&gt;
				@Html.EditorFor(x =&gt; x.ClonePosts[i].ItemData)
			&lt;/td&gt;
			&lt;td&gt;
				@Html.EditorFor(x =&gt; x.ClonePosts[i].ItemSelected)
			&lt;/td&gt;
			&lt;td&gt;
				&lt;input type=&quot;submit&quot; value=&quot;Clone&quot; /&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
	 }	  
  }   
&lt;/tbody&gt;
&lt;/table&gt;

Your Controller method will be:

public async Task&lt;IActionResult&gt;ReviewClone(string zero = &quot;230323-0&quot;)
{
	...
	ReviewCloneVM model=new ReviewCloneVM();
	// now call up the full table for review and edit
	var cloned = _context.ClonePosts.OrderByDescending(m =&gt; m.PostNumber);
	model.ClonePosts =await cloned.ToListAsync();
	return View(model);
}

答案2

得分: 0

在你的视图中,你需要使用以下代码部分作为模型:

@model AURA.ViewModels.ReviewCloneVM

所以尝试修改你的代码如下:

Controller:

public async Task<IActionResult> ReviewClone(string zero = "230323-0")
{
    // 现在调用完整的表格进行审查和编辑
    var cloned = _context.ClonePosts.OrderByDescending(m => m.PostNumber).ToList();
    ReviewCloneVM vm = new ReviewCloneVM()
    {
        ClonePosts = cloned,
    };
    return View(vm);
}

View:

@model AURA.ViewModels.ReviewCloneVM

<h1>ReviewClone</h1>
<form method="post">
    @for (int i = 0; i < Model.ClonePosts.Count; i++)
    {
        <tr>
            <td>
                @Html.EditorFor(x => x.ClonePosts[i].PostNumber)
            </td>
            <td>
                @Html.EditorFor(x => x.ClonePosts[i].ItemNumber)
            </td>
            <td>
                @Html.EditorFor(x => x.ClonePosts[i].ItemData)
            </td>
            <td>
                @Html.EditorFor(x => x.ClonePosts[i].ItemSelected)
            </td>
        </tr>
        <br />
    }

    <td>
        <input type="submit" value="Save" />
    </td>
</form>

演示:

[![enter image description here][1]][1]


<details>
<summary>英文:</summary>

In your View, You need to use 

    @model AURA.ViewModels.ReviewCloneVM
as the model, So try to change your  code like:

**Controller**

    public async Task&lt;IActionResult&gt; ReviewClone(string zero = &quot;230323-0&quot;)
            {
                
            //now call up the full table for review and edit
                var cloned =  _context.ClonePosts.OrderByDescending(m =&gt; m.PostNumber).ToList();
                ReviewCloneVM vm = new ReviewCloneVM()
                {
                    ClonePosts = cloned,
                };
                return View(vm);
            }

**View**

    @model AURA.ViewModels.ReviewCloneVM
    
    &lt;h1&gt;ReviewClone&lt;/h1&gt;
    &lt;form method=&quot;post&quot;&gt;
        @*foreach (var item in Model)*@
        @for (int i = 0; i &lt; Model.ClonePosts.Count; i++)
        {
            &lt;tr&gt;
                &lt;td&gt;
                    @Html.EditorFor(x =&gt; x.ClonePosts[i].PostNumber)
                &lt;/td&gt;
                &lt;td&gt;
                    @Html.EditorFor(x =&gt; x.ClonePosts[i].ItemNumber)
                &lt;/td&gt;
                &lt;td&gt;
                    @Html.EditorFor(x =&gt; x.ClonePosts[i].ItemData)
                &lt;/td&gt;
                &lt;td&gt;
                    @Html.EditorFor(x =&gt; x.ClonePosts[i].ItemSelected)
                &lt;/td&gt;
                
            &lt;/tr&gt;
            &lt;br /&gt;
        }
    
        &lt;td&gt;
            &lt;input type=&quot;submit&quot; value=&quot;Save&quot; /&gt;
        &lt;/td&gt;
       
    &lt;/form&gt;

**Demo**:

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/j5azD.gif


</details>



huangapple
  • 本文由 发表于 2023年3月23日 12:48:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定