Problem with displaying the list asp.net core mvc 3.0 in page.

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

Problem with displaying the list asp.net core mvc 3.0 in page

问题

I had the following problem:
我遇到了以下问题:

I need the user to fill in several fields (the work he performs and indicate the price for this work):
我需要用户填写多个字段(他从事的工作以及为此工作指定价格):

Since there are several such jobs, I use the list, but I get the following error:
由于有多个这样的工作,我使用列表,但是我遇到了以下错误:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index'):
ArgumentOutOfRangeException:索引超出范围。必须为非负数且小于集合大小。(参数“index”):

This error appears as soon as I go to the "edit" page:
这个错误出现在我转到“编辑”页面后。

Here is my controller code:
以下是我的控制器代码:

User user = await _userManager.FindByNameAsync(User.Identity.Name);
foreach(var up in _context.TutorProfesionAndCosts.ToList())
{
if (user.Id == up.UserId)
user.ProfesionAndCosts.Add(up);
}

User models:
用户模型:

public List ProfesionAndCosts { get; set; }

public User()
{
ProfesionAndCosts = new List();
}

Work models:
工作模型:

public class TutorProfesionAndCost
{
public int Id { get; set; }

public string UserId { get; set; }
public User User { get; set; }

public string EducationalSubject { get; set; }

public int Cost_One { get; set; } 
public int Cost_Group { get; set; } 

public string Currency { get; set; }

}

Edit page:
编辑页面:

@{int i = 0;}


Individual session:
Group session:

<button onclick="addSubject()">New</button>

JavaScript for adding a new subject:
用于添加新主题的JavaScript:

英文:

I had the following problem:
I need the user to fill in several fields (the work he performs and indicate the price for this work)
Since there are several such jobs, I use the list, but I get the following error:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

This error appears as soon as I go to the "edit" page.

Here is my controller code:

        User user = await _userManager.FindByNameAsync(User.Identity.Name);
        foreach(var up in _context.TutorProfesionAndCosts.ToList())
        {
            if (user.Id == up.UserId)
                user.ProfesionAndCosts.Add(up);
        }

User models

    public List&lt;TutorProfesionAndCost&gt; ProfesionAndCosts { get; set; }

    public User()
    {
        ProfesionAndCosts = new List&lt;TutorProfesionAndCost&gt;();
    }

Models of "work"

public class TutorProfesionAndCost
{
    public int Id { get; set; }

    public string UserId { get; set; }
    public User User { get; set; }
    
    public string EducationalSubject { get; set; }

    public int Cost_One { get; set; } 
    public int Cost_Group { get; set; } 

    public string Currency { get; set; }
}

edit page

@{int i = 0;} 

&lt;p&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col&quot;&gt;
            &lt;label class=&quot;mb-0&quot;&gt;Оберіть предмет який ви викладаєте:&lt;/label&gt;
            &lt;select asp-for=&quot;@Model.Tutor.ProfesionAndCosts[@i].EducationalSubject&quot; required=&quot;required&quot;&gt;
                @foreach (var profesion in Model.educational_Subject)
                {
                    &lt;option value=&quot;@profesion.EducationalSubject&quot;&gt;@profesion.EducationalSubject&lt;/option&gt;
                }
            &lt;/select&gt;
        &lt;/div&gt;
        &lt;div class=&quot;col&quot;&gt;
            &lt;label&gt;Вкажіть вартість за 1 годину:&lt;/label&gt;
            &lt;p&gt;
                &lt;span&gt;Індивідуальне заняття: &lt;input class=&quot;money&quot; type=&quot;text&quot; asp-for=&quot;@Model.Tutor.ProfesionAndCosts[@i].Cost_One&quot; /&gt; &lt;/span&gt;
                &lt;span&gt;Групове заняття: &lt;input class=&quot;money&quot; type=&quot;text&quot; asp-for=&quot;@Model.Tutor.ProfesionAndCosts[@i].Cost_Group&quot; /&gt; &lt;/span&gt;
                &lt;select asp-for=&quot;@Model.Tutor.ProfesionAndCosts[@i].Currency&quot;&gt;
                    @foreach (var cur in Model.currency)
                    {
                        &lt;option value=&quot;@cur.CurrencyMoney&quot;&gt;@cur.CurrencyMoney&lt;/option&gt;
                    }
                &lt;/select&gt;
            &lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;button onclick=&quot;addSubject()&quot;&gt;New&lt;/button&gt;
&lt;/p&gt;

javascript new subject

&lt;script&gt;
function addSubject() {
    var index = document.querySelectorAll(&#39;.row&#39;).length;
    var html = `&lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col&quot;&gt;
            &lt;label class=&quot;mb-0&quot;&gt;Оберіть предмет який ви викладаєте:&lt;/label&gt;
            &lt;select asp-for=&quot;@@Model.Tutor.ProfesionAndCosts[${index}].EducationalSubject&quot; required=&quot;required&quot;&gt;
                @foreach (var profesion in Model.educational_Subject)
                {
                    &lt;option value=&quot;@profesion.EducationalSubject&quot;&gt;@profesion.EducationalSubject&lt;/option&gt;
                }
            &lt;/select&gt;
        &lt;/div&gt;
        &lt;div class=&quot;col&quot;&gt;
            &lt;label&gt;Вкажіть вартість за 1 годину:&lt;/label&gt;
            &lt;p&gt;
                &lt;span&gt;Індивідуальне заняття: &lt;input class=&quot;money&quot; type=&quot;text&quot; asp-for=&quot;@@Model.Tutor.ProfesionAndCosts[${index}].Cost_One&quot; /&gt; &lt;/span&gt;
                &lt;span&gt;Групове заняття: &lt;input class=&quot;money&quot; type=&quot;text&quot; asp-for=&quot;@@Model.Tutor.ProfesionAndCosts[${index}].Cost_Group&quot; /&gt; &lt;/span&gt;
                &lt;select asp-for=&quot;@@Model.Tutor.ProfesionAndCosts[${index}].Currency&quot;&gt;
                    @foreach(var cur in Model.currency)
                    {
                        &lt;option value=&quot;@cur.CurrencyMoney&quot;&gt;@cur.CurrencyMoney&lt;/option&gt;
                    }
                &lt;/select&gt;
            &lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;`;
    $(&#39;#subjects&#39;).append(html);
    i = index + 1;
}
&lt;/script&gt;

答案1

得分: 1

The problem was solved. Instead of "asp-for" I passed the value using "name"

<div id="ProfesionBlock">
    <div class="Profesion">
        <div class="row">
            <div class="col">
                <p class="mb-0">Оберіть предмет який ви викладаєте:</p>
                <input type="hidden" name="ProfesionAndCosts[0].UserId" value="@Model.Tutor.Id" />
                <select name="ProfesionAndCosts[0].EducationalSubject" required="required">
                    @foreach (var profesion in Model.educational_Subject)
                    {
                        <option value="@profesion.EducationalSubject">@profesion.EducationalSubject</option>
                    }
                </select>
            </div>
            <div class="col">
                <p>Вкажіть вартість за 1 годину:</p>
                <div>
                    <p>Індивідуальне заняття: <input class="money" type="text" name="ProfesionAndCosts[0].Cost_One" /> </p>
                    <p>Групове заняття: <input class="money" type="text" name="ProfesionAndCosts[0].Cost_Group" /> </p>
                    <label>Оберіть валюту: </label>
                    <select name="ProfesionAndCosts[0].Currency">
                        @foreach (var cur in Model.currency)
                        {
                            <option value="@cur.CurrencyMoney">@cur.CurrencyMoney</option>
                        }
                    </select>
                </div>
            </div>
        </div>
    </div>
</div>
<p>
    <a class="addNewProfesion">Добавити нову професію</a>
</p>

@section Scripts {
<script>
    $(function () {
        var i = 0;
        $('.addNewProfesion').click(function () {
            i++;
            var html2Add = `<div class="row">
            <div class="col">
                <p class="mb-0">Оберіть предмет який ви викладаєте:</p>
                <select name="ProfesionAndCosts[${i}].EducationalSubject" required="required">
                    @foreach (var profesion in Model.educational_Subject)
                    {
                        <option value="@profesion.EducationalSubject">@profesion.EducationalSubject</option>
                    }
                </select>
            </div>
            <div class="col">
                <label>Вкажіть вартість за 1 годину:</label>
                <p>
                    <p>Індивідуальне заняття: <input class="money" type="text"  name="ProfesionAndCosts[${i}].Cost_One" /> </p>
                    <p>Групове заняття: <input class="money" type="text" name="ProfesionAndCosts[${i}].Cost_Group" /> </p>
                    <select name="ProfesionAndCosts[${i}].Currency">
                        @foreach(var cur in Model.currency)
                        {
                            <option value="@cur.CurrencyMoney">@cur.CurrencyMoney</option>
                        }
                    </select>
                </p>
            </div>
        </div>`;
            $('#ProfesionBlock').append(html2Add);
        })
    })
</script>
}

Controller

public async Task<IActionResult> AccountEditTutor(List<TutorProfesionAndCost> ProfesionAndCosts)
{
    var userProf = await _context.TutorProfesionAndCosts
        .Where(up => up.UserId == user.Id)
        .ToListAsync();
    _context.TutorProfesionAndCosts.RemoveRange(userProf);

    foreach(var userProfAdd in ProfesionAndCosts)
    {
        var up = new TutorProfesionAndCost
        {
            UserId = user.Id,
            EducationalSubject = userProfAdd.EducationalSubject,
            Cost_One = userProfAdd.Cost_One,
            Cost_Group = userProfAdd.Cost_Group,
            Currency = userProfAdd.Currency
        };
        await _context.TutorProfesionAndCosts.AddAsync(up);
    }
    ///....
}

The code is not the best but it works.

英文:

The problem was solved. Instead of "asp-for" I passed the value using "name"

&lt;div id=&quot;ProfesionBlock&quot;&gt;
&lt;div class=&quot;Profesion&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col&quot;&gt;
&lt;p class=&quot;mb-0&quot;&gt;Оберіть предмет який ви викладаєте:&lt;/p&gt;
&lt;input type=&quot;hidden&quot; name=&quot;ProfesionAndCosts[0].UserId&quot; value=&quot;@Model.Tutor.Id&quot; /&gt;
&lt;select name=&quot;ProfesionAndCosts[0].EducationalSubject&quot; required=&quot;required&quot;&gt;
@foreach (var profesion in Model.educational_Subject)
{
&lt;option value=&quot;@profesion.EducationalSubject&quot;&gt;@profesion.EducationalSubject&lt;/option&gt;
}
&lt;/select&gt;
&lt;/div&gt;
&lt;div class=&quot;col&quot;&gt;
&lt;p&gt;Вкажіть вартість за 1 годину:&lt;/p&gt;
&lt;div&gt;
&lt;p&gt;Індивідуальне заняття: &lt;input class=&quot;money&quot; type=&quot;text&quot; name=&quot;ProfesionAndCosts[0].Cost_One&quot; /&gt; &lt;/p&gt;
&lt;p&gt;Групове заняття: &lt;input class=&quot;money&quot; type=&quot;text&quot; name=&quot;ProfesionAndCosts[0].Cost_Group&quot; /&gt; &lt;/p&gt;
&lt;label&gt;Оберіть валюту: &lt;/label&gt;
&lt;select name=&quot;ProfesionAndCosts[0].Currency&quot;&gt;
@foreach (var cur in Model.currency)
{
&lt;option value=&quot;@cur.CurrencyMoney&quot;&gt;@cur.CurrencyMoney&lt;/option&gt;
}
&lt;/select&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a class=&quot;addNewProfesion&quot;&gt;Добавити нову професію&lt;/a&gt;
&lt;/p&gt;
@section Scripts {
&lt;script&gt;
$(function () {
var i = 0;
$(&#39;.addNewProfesion&#39;).click(function () {
i++;
var html2Add = `&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col&quot;&gt;
&lt;p class=&quot;mb-0&quot;&gt;Оберіть предмет який ви викладаєте:&lt;/p&gt;
&lt;select name=&quot;ProfesionAndCosts[${i}].EducationalSubject&quot; required=&quot;required&quot;&gt;
@foreach (var profesion in Model.educational_Subject)
{
&lt;option value=&quot;@profesion.EducationalSubject&quot;&gt;@profesion.EducationalSubject&lt;/option&gt;
}
&lt;/select&gt;
&lt;/div&gt;
&lt;div class=&quot;col&quot;&gt;
&lt;label&gt;Вкажіть вартість за 1 годину:&lt;/label&gt;
&lt;p&gt;
&lt;p&gt;Індивідуальне заняття: &lt;input class=&quot;money&quot; type=&quot;text&quot;  name=&quot;ProfesionAndCosts[${i}].Cost_One&quot; /&gt; &lt;/p&gt;
&lt;p&gt;Групове заняття: &lt;input class=&quot;money&quot; type=&quot;text&quot; name=&quot;ProfesionAndCosts[${i}].Cost_Group&quot; /&gt; &lt;/p&gt;
&lt;select name=&quot;ProfesionAndCosts[${i}].Currency&quot;&gt;
@foreach(var cur in Model.currency)
{
&lt;option value=&quot;@cur.CurrencyMoney&quot;&gt;@cur.CurrencyMoney&lt;/option&gt;
}
&lt;/select&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;`;
$(&#39;#ProfesionBlock&#39;).append(html2Add);
})
})
&lt;/script&gt;

Controller

public async Task&lt;IActionResult&gt; AccountEditTutor( List&lt;TutorProfesionAndCost&gt; ProfesionAndCosts)
{
var userProf = await _context.TutorProfesionAndCosts
.Where(up =&gt; up.UserId == user.Id)
.ToListAsync();
_context.TutorProfesionAndCosts.RemoveRange(userProf);
foreach(var userProfAdd in ProfesionAndCosts)
{
var up = new TutorProfesionAndCost
{
UserId = user.Id,
EducationalSubject = userProfAdd.EducationalSubject,
Cost_One = userProfAdd.Cost_One,
Cost_Group = userProfAdd.Cost_Group,
Currency = userProfAdd.Currency
};
await _context.TutorProfesionAndCosts.AddAsync(up);
}
///....
}

The code is not the best but it works

huangapple
  • 本文由 发表于 2023年4月13日 16:19:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003199.html
匿名

发表评论

匿名网友

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

确定