0

I am just trying to fill my Model with the View values and all the model values are still empty. And every method that I tried to use from Tutorials didn't work. I can set the Values from the Model but I can't fill things like Drehrichtung which is bound on a view value.

Here is my Model:

 namespace PlaudertischSoftware.Models
{
    public class PlauderViewModel
    {
        public string Bild { get; set; }
        public bool IstObst { get; set; }
        public int Richtig { get; set; }
        public int Falsch { get; set; }
        public int Drehrichtung { get; set; }
        public int BestritteneRunden { get; set; }
    }
}

Here is my Controller:

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Collections.Specialized;
using PlaudertischSoftware.Models;
using PlaudertischSoftware.Sensoren;
using System.Linq;

namespace PlaudertischSoftware.Controllers
{
    public class PlauderController : Controller

    public ActionResult SpielView()
        {
            PlauderViewModel model = new PlauderViewModel();
            int index = 0;

            model.Bild = GetBild(out index);
            //TODO: Falls neue Bilder hinzu kommen, muss das hier aktualisiert werden
            if (index > 0 && index < 15)
            {
                model.IstObst = true;
            }
            else
            {
                model.IstObst = false;
            }

            var varfrut = new KeyValuePair<string, bool>();



            if (varfrut.Value == true && model.Drehrichtung > 0)
            {
                model.Richtig = model.Richtig++;
                model.Falsch = model.Falsch;
            }
            else
            {
                if (varfrut.Value == false && model.Drehrichtung < 0)
                {
                    model.Richtig = model.Richtig++;
                    model.Falsch = model.Falsch;
                }
                else
                {
                    model.Falsch = model.Falsch++;
                    model.Richtig = model.Richtig;
                }



                if (model.Falsch + model.Richtig == 9)
                {
                    return View("AuswertungsView", model);
                }
            }

            return View(model);
        }
        private string GetBild(out int index)
        {
            Random random = new Random();
            var list = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" };
            index = random.Next(list.Count);



            string bild = "~/Bilder/" + list[index] + ".jpg";

            return bild;
        }
         [HttpPost]
        public ActionResult TimerStopped(/*string customModel, */PlauderViewModel model)
        {

            //PlauderViewModel model = null;
            int index;

            string schwuchtelMICROSCHROTT = Request["txtRunde"].ToString();

            model.Bild = Url.Content(GetBild(out index));

            ObstSpielDaten obstSpielDaten = null;
            try
            {
                using (DatenbankKontext dbKontext = new DatenbankKontext())
                {
                    try
                    {
                        obstSpielDaten = dbKontext.ObstSpielDaten.FirstOrDefault();
                    }
                    catch (Exception ex)
                    {
                        //TODO: hier fehler abfangen
                    }

                    //Auswerten
                    AntwortAuswerten(ref model);

                    //Angleichen und berechnen der übergeben View Daten in der Datenbank
                    if (obstSpielDaten == null)
                    {
                        obstSpielDaten = new ObstSpielDaten(DBHelper.GetNextObstSpielDatenID());
                        obstSpielDaten.FalschAnzahl = model.Falsch;
                        obstSpielDaten.RichtigAnzahl = model.Richtig;

                        try
                        {
                            dbKontext.ObstSpielDaten.Add(obstSpielDaten);
                            int anzahlAenderungen = dbKontext.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            //TODO: hier fehler abfangen
                        }
                    }
                    else
                    {

                    }
                }

            }
            catch (Exception ex)
            {

            }

            return Json(model);
        }
    }
}

Here is my View:

@using DevExtreme.AspNet.Mvc;

@section Styles {
    <link href="@Url.Content("~/Content/design.css")" rel="stylesheet" type="text/css" />
}
@using (Html.BeginForm("SpielView", "Plauder", FormMethod.Post))
{
    <h1 align="center" style="font-weight:bold">Ist hier Obst zu sehen?</h1>
    <div align="center" id="Bild_und_Counter">
        <img id="imgFruchtBild" style="position:center; border:ridge; border-color:#3f464c" src="@Url.Content(Model.Bild)" width="600" height="450" />
    </div>
    <div id="counter" align="center">
        <input type="button" name="CounterR" value="richtig:" style="font-size:x-large; font-weight:bold; width:85px; text-align:center; background-color:orange; border:thick; border-color:orange" />
        <input id="ipRichtig" type="number" name="ipRichtig" value="@Model.Richtig" style="font-size:x-large; font-weight:bold; width:35px; text-align:center; background-color:orange; border:thick; border-color:orange; margin-right:80px">
        <input type="button" name="CounterF" value="falsch:" style="font-size:x-large; font-weight:bold; width:85px; text-align:center; background-color:orangered; border:thick; border-color:orangered" />
        <input id="ipFalsch" type="number" name="ipFalsch" value="@Model.Falsch" style="font-size:x-large; font-weight:bold; width:35px; text-align:center; background-color:orangered; border:thick; border-color:orangered" />
    </div>
    <div class="lineargauge">
        @(Html.DevExtreme().LinearGauge()

     .ID("lineargauge")
     .Scale(s => s
     .StartValue(-20)
     .EndValue(20)
     .TickInterval(20)
     .Tick(t => t.Color("#536878"))
     .Label(l => l.CustomizeText(@<text>
     function(arg){
     if(arg.value > 0){
     return "Ja";
      }
     else if(arg.value < 0 ){
     return "Nein";
      }
     else{
     return "0";
      }
     }
     </text>)
    )
  )
     .RangeContainer(r => r
     .Offset(10)
     .Ranges(c =>
     {
      c.Add().StartValue(-20).EndValue(0).Color("orangered");
      c.Add().StartValue(0).EndValue(20).Color("orange");
     })
      )
     .ValueIndicator(v => v.Offset(20))
     .SubvalueIndicator(s => s.Offset(-15))
     .Export(e => e.Enabled(true))
     .Tooltip(x => x
     .Enabled(true)

)
    .Title(t => t
    .Font(f => f.Size(28))
)

     .Value(new JS("drehen"))
     .ValueIndicator(i => i
     .BaseValue(@Model.Drehrichtung)
     .Type(GaugeIndicatorType.TriangleMarker)
     .Color("#3f464c")
)
        )
    </div>
    <div class="form">
        @(Html.DevExtreme().Button()

     .ID("progress-button")
     .Text("Starten")
     .Width(120)
     .OnClick("button_onClick")
        )
    </div>
    <div class="progress-info">
        <br />
        <span>Runde: <input id="txtRunde" type="number" value="@Model.BestritteneRunden" style="width: 15px; border-width: 0px"/>von 10</span>
        <br />
        Übrige Zeit 00:00:<span id="timer">10</span>
    </div>

    <div id="progress-info">
        @(Html.DevExtreme().ProgressBar()

    .ID("progressBarStatus")
    .Min(0)
    .Max(100)
    .Width("100%")
    .Height(50)
    .StatusFormat(new JS("progressBar_statusFormat"))
    .OnComplete("progressBar_onComplete")
    .OnValueChanged("progressBar_valueChanged")
        )

        <div>
            @(Html.DevExtreme().Slider()

    .ID("handler-slider")
    .Min(-20)
    .Max(20)
    .Value(new JS("drehen"))
    .Width(1140)
    .Visible(false)
    .OnValueChanged(@<text>

     function(e){
     $("#lineargauge").dxLinearGauge("instance").value(e.value);
}
</text>)
            )
        </div>
    </div>
    <div class="dx-field-label">Anzahl der Umdrehungen</div>
    <div class="dx-field-value">
        @(Html.DevExtreme().NumberBox()

    .ID("slider-value")
    .InputAttr(false)
    .Value(new JS("drehen"))
    .Min(-20)
    .Max(20)
    .Width(200)
    .ShowSpinButtons(true)
    .OnValueChanged("numberBox_valueChanged")

        )
    </div>
6
  • 1
    I see that you have not set a value for Drehrichtung property in Controller (SpielView action). In order to use any property, first you have to set a value to it in Controller. Commented Jan 22, 2020 at 12:46
  • And how do I do this? Commented Jan 22, 2020 at 13:07
  • 1
    With the same way you set Bild, IsObst, Richtig and Falsch properties. You have set them all but not Drehrictung property. Commented Jan 22, 2020 at 13:43
  • But can I set the Drehrichtung value as same as the drehen value in my view? Commented Jan 22, 2020 at 14:53
  • 1
    I am not sure if I get your point right but if you want to update a model or pass any data to an Action of a Controller, you have to post these values from view by submitting a form. Please see this tutorial page for reference. learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/… Commented Jan 22, 2020 at 17:05

1 Answer 1

1

You are not referencing model in your view. Below line of code will work at the beginning of your view:

@model PlaudertischSoftware.Models.PlauderViewModel

Put appropriate model in declaration.

Sign up to request clarification or add additional context in comments.

1 Comment

But the problem remains still the same. The value "model.Drehrichtung" which is bound to "drehen" in my view won't still populate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.