如何将秒转换为分钟并在标签后立即显示?

How to convert seconds to minute and show it right after in label?

我需要我的用户输入秒数作为输入,但在用户输入后我想立即将秒数转换为分钟数并在标签中显示给用户。我在这里错过了什么?

查看

@Html.BSMaterialTextBoxFor(model => model.seconds, "Enter the seconds",colCSS: "col-6", htmlAttributes: new { data_bindrop = "seconds" , autocomplete = "off" })
@Html.LabelFor(model => model.secondsStr)

ViewModel

  public int seconds{ get; set; }

  [Display(Name = "Time")]
   public string secondsStr
        {
            get
            {
                return (seconds/ 60).ToString() +" Minutes"+ " " + (seconds% 60).ToString()+"Seconds";
            }
        }         

时间旁边应该写0分45秒

There are even easier way to handle what you are trying to achieve. I hope this is what you are looking for:

请按照以下步骤操作:

Model:

public class SecondModel
    {
        public double Seconds { get; set; }
    }

Controller For Display:

 public IActionResult Index()
        {

            ViewBag.Minutes = TempData["Minutes"];
            return View();
           
        }

Controller When Submit:

        [HttpPost]
        public async Task<IActionResult> AddSecond(SecondModel second)
        {
            TimeSpan t = TimeSpan.FromSeconds(second.Seconds);
            string convertedMinutes = string.Format("{0:D2}m:{1:D2}s", t.Minutes, t.Seconds);
            TempData["Minutes"] = convertedMinutes;
            return RedirectToAction("Index");
        }

Note: Point to rememebr in C# Or asp.net core there is a built in method to do that. I am just using TimeSpan.FromSeconds to implement that.

View:

@model DotNet6MVCWebApp.Models.SecondModel
<div class="row">
    <div class="form-group">
        <form class="form-group" method="post" asp-controller="Second" asp-action="AddSecond">
            <table  style="border-spacing: 12px;">
                <tr>
                    <td><strong>Enter Seconds</strong></td>
                    <td><input asp-for="Seconds" class="form-control" /></td>
                    <td><button type="submit" class="btn btn-primary">Convert Second In Minutes</button></td>
                    <td>You have Entered-<strong>@ViewBag.Minutes</strong></td>
                </tr>
            </table>
        </form>

    </div>
</div>

Output: