Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a View that contains n number of input fields (the number will vary based on different criteria).

(我有一个包含n个输入字段的视图(该数量将根据不同的条件而有所不同)。)

the value of each input field needs to be inserted in the database in their own row.

(每个输入字段的值都需要在数据库中的自己行中插入。)

My problem is that only the first input field is inserted into the database.

(我的问题是只有第一个输入字段插入到数据库中。)

The controller looks like this:

(控制器如下所示:)

public ActionResult Create([Bind(Include = "Id,MemberId,Rated,Rating")] Rating rating)
        {
                if (ModelState.IsValid)
                {
                    db.Ratings.Add(rating);
                    db.SaveChanges();                  
                }

The value of MemberId is the same for each row, but Rated and Rating will be different.

(每行的MemberId值均相同,但“额定”和“额定”将有所不同。)

My model looks like this:

(我的模型如下所示:)

[Table("Rating")]
    public partial class Rating
    {
        public int Id { get; set; }

        public int? MemberId { get; set; }

        public int Rated { get; set; }

        [Column("Rating")]
        public int Rating { get; set; }

        public virtual TeamMember TeamMember { get; set; }
    }

The view looks like this:

(该视图如下所示:)

@model Teamer.Models.Rating

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Rating</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @{
                int count = 0;

                foreach (var item in ViewBag.MemberId)
                {
                    string rater = Request.Cookies["rater"]["name"].ToString();
                    string raterId = Request.Cookies["rater"]["raterId"];

                    if (item.Name.ToLower() != rater.ToLower())
                    {
                        if (ViewBag.raterId != null)
                        {
                            foreach (var raterid in ViewBag.raterId)
                            {
                                <input type="hidden" name="MemberId" value="@raterid" />
                            }
                        }
                        <div class="col-md-10">
                            <label class="control-label col-md-2" for="@item.Name">@item.Name</label>
                            <input type="number" name="Rating-@count" value="0" />
                            <input type="hidden" name="Rated" value="@item.Id" />
                        </div>  
                        count++;
                    }
                }
            }
        </div>

        <input type="hidden" name="count" value="@count" />

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

I'm guessing, I need to loop through the values one by one, but I can't get that to work, so maybe I'm way off on that.

(我猜想,我需要一个接一个地循环遍历这些值,但是我无法使它正常工作,所以也许我对此不太满意。)

  ask by Lasserh translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
220 views
Welcome To Ask or Share your Answers For Others

1 Answer

I can see a couple of problems, but it the end, you're not posting what is expected by your controller.

(我可以看到一些问题,但最后,您没有发布控制器期望的内容。)

Your controller is expecting to get a Rating (so Id, MemberId, Rated, and Rating), but you're actually posting something like

(您的控制器希望获得Rating (因此ID,MemberId,Rated和Rating),但实际上您发布的是)

MemberId=raterId (repeated by as many ViewBag.raterId you have)
Rating-0=0
Rated=memberX_Id
Rating-1=0
Rated=memberY_Id
Rating-2=0
Rated=memberZ_Id
(...)
Rating-n=0
Rated=memberM_Id
count=(n+1)

If you check your posted data, you should have only MemberId and Rated filled.

(如果您检查发布的数据,则应该只填写MemberId和Rating。)

In order to achieve what you want, you'll need to have a list of Rating or a different structure on your controller side.

(为了实现所需的功能,您需要在控制器端具有一个Rating列表或其他结构。)

Bear in mind, if you want to post an array, it should be something like

(请记住,如果要发布数组,它应该类似于)

MemberId[0]=
Rated[0]=
Rating[0]=
MemberId[1]=
Rated[1]=
Rating[1]=
(...)
MemberId[n]=
Rated[n]=
Rating[n]=

Also, note that MemberId and Rated get overridden in your loops.

(另外,请注意,MemberId和Rating在循环中被覆盖。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...