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

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

asp.net mvc - Populated on MVC form View the TextBox

I need populate on MVC form View the TextBox below

@Html.LabelFor(m => m.tDate)
@Html.TextBoxFor(m => m.tDate, "{0:dd/MM/yyyy}", new { @class = "Mytextarea2" })
@Html.ValidationMessageFor(m => m.tDate, "", new { @class = "text-danger" })

Whit value extract from controller (using MySql database) when I have planned this code

public DateTime JsonDateTimeToNormal(string jsonDateTime)
{
    jsonDateTime= @"""" + jsonDateTime + @"""";
    return Newtonsoft.Json.JsonConvert.DeserializeObject<DateTime>(jsonDateTime);
}

...

model.tDate = JsonDateTimeToNormal(GetDateHourById(value).ToString());

...

private static string GetDateHourById(string value)
{
    string sql;
    string constr = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        sql = @String.Format(" SELECT ");
        sql += String.Format(" tDate ");
        sql += String.Format(" FROM `dotable` ");
        sql += String.Format(" WHERE tID = @Id;");

        using (MySqlCommand cmd = new MySqlCommand(sql))
        {
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Id", value);
            con.Open();
            string name = Convert.ToString(cmd.ExecuteScalar());
            con.Close();

            return name;
        }
    }
}

The value memorized on MySql database is

2020-03-19

But the TextBox is populed with this value

/Date(1605481200000)/

On the model

[Column(TypeName = "date")]
[Display(Name = "Your date")]
[Required]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? tDate { get; set; }

Help me to do it.

Debug VS 2109

enter image description here

Solution

const d = new Date(response.tDate.match(/d+/)[0] * 1);
const formattedDate = d.getFullYear()+'-'+("0"+(d.getMonth()+1)).slice(-2)+'-'+("0"+d.getDate()).slice(-2)
$('#tDate').val( formattedDate );
question from:https://stackoverflow.com/questions/65846721/populated-on-mvc-form-view-the-textbox

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

1 Answer

0 votes
by (71.8m points)

why don't you try to make this call with a jquery?

view:

@Html.TextBoxFor(m => m.tDate, "{0:dd/MM/yyyy}", new { @class = "Mytextarea2" })

$("#document").on("click", ".YouFormCall", function () {
                var date = new Object();
                date.tDate = $(this).closest('tr').find(".Mytextarea2").val();

controller:

        yourModel date= new yourModel ()
        {
            tDate= date.tDate,
        };
        db.Entry(date).State = EntityState.Modified;
        db.SaveChanges();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...