将默认表单字段值传递给Database MVC4

时间:2015-05-19 10:11:57

标签: c# asp.net-mvc forms asp.net-mvc-4 methods

我创建了一个供用户编辑用户数据的网站,表单字段预先填入了他们当前的数据,他们可以编辑他们想要的内容。

我的问题是,在我的表单中使用post方法时,它只传递更改的字段,而不是默认值的字段,如何让它也将默认值传递给我的控制器?

来自表单的编辑代码

<table class="table table-condensed table-hover">
  <tr>
    <th>Fornavn</th>

    <td>
      <input name="Fornavn" class="center-content" type="text" value="@row[" FirstName "]" required>
    </td>
  </tr>
  <tr>
    <th>Efternavn</th>

    <td>
      <input name="Efternavn" class="center-content" type="text" value="@row[" LastName "]" required>
    </td>
  </tr>
  <tr>
    <th>Email</th>

    <td>
      <input name="Email" class="center-content" type="text" value="@row[" Email "]" required>
    </td>
  </tr>


  <tr>
    <th>Telefon</th>

    <td>
      <input name="Telefonnummer" class="center-content" type="text" value="@row[" PhoneNumber "]" required>
    </td>
  </tr>

  <tr>
    <th>Avatar</th>

    <td>
      <input id="file" type="file" name="file">
      <br />
      <p style="font-size:11px">
        For bedste resultat, venligst brug et billede i størrelse 200x200 PX. (Ellers resizer vi det for dig, og det ser herrens ud.)
      </p>
    </td>
  </tr>
</table>

</div>


<div class="col-md-4">
  <h4>Adresse</h4>
  <br>


  <table class="table table-condensed table-hover">
    <tr>
      <th>Vejnavn</th>

      <td>
        <input name="Road" class="center-content" type="text" value="@row[" Road "]" required>
      </td>
    </tr>


    <tr>
      <th>Vejnummer</th>

      <td>
        <input name="RoadNumber" class="center-content" type="text" value="@row[" RoadNumber "]" required>
      </td>
    </tr>


    <tr>
      <th>By</th>

      <td>
        <input name="Town" class="center-content" type="text" value="@row[" Town "]" required>
      </td>
    </tr>


    <tr>
      <th>Postnummer</th>

      <td>
        <input name="ZipCode" class="center-content" type="text" value="@row[" ZipCode "]" required>
      </td>
    </tr>


    <tr>
      <th>Land</th>

      <td>
        <input name="Country" class="center-content" type="text" value="@row[" Country "]" required>
      </td>
    </tr>
  </table>
</div>
<div class="col-md-2">
</div>
</div>
<div class="row">
  <div class="col-md-2"></div>
  <div class="col-md-4">
    <br />
    <br />
    <a href="@Url.Action(" UserProfile ", "Home ")" class="button button-big">
      <img src="@Url.Content(" ~/Content/img/ui-icon/corporate-w.png ")" style="height: 35px; margin-top: -6px;" />Tilbage til min profil!</a>
    <br />
    <br />
  </div>
  <div class="col-md-4">
    <br />
    <br />
    <button type="submit" value="Upload" class="button button-big">
      <img src="@Url.Content(" ~/Content/img/ui-icon/corporate-w.png ")" style="height: 35px; margin-top: -6px;" />Gem Mine Ændringer!</button>
    <br />
    <br />
  </div>
  <div class="col-md-2"></div>
</div>
</div>
</form>

控制器

public ActionResult EditPrivateUser(EventCrushV001.Models.PrivateUser user, HttpPostedFileBase file, string path, string sqlPath)
    {
        if (file != null && file.ContentLength > 0)
            try
            {
                var PID = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                path = Path.Combine(Server.MapPath("~/Upload/Avatar"), PID);
                sqlPath = Path.Combine(("../Upload/Avatar"), PID);
                file.SaveAs(path);
                ViewBag.Message = "File uploaded successfully";
            }
            catch (Exception ex)
            {
                ViewBag.Message = "ERROR:" + ex.Message.ToString();
            }
        else
        {
            ViewBag.Message = "You have not specified a file.";
        }

        if (!ModelState.IsValid)
        {
            // Error handling here
        }

        var con = GetDefaultConnection();
        MySqlCommand cmd = new MySqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Update Private_User Set (PW=@PW, Birthdate=@Birthdate, FirstName=@FirstName, LastName=@LastName, Email=@Email, PhoneNumber=@PhoneNumber, Avatar=@Avatar) Where (UserID=@UserID)";
        cmd.Parameters.AddWithValue("@PW", user.Password);
        cmd.Parameters.AddWithValue("@Birthdate", user.Fødselsdag);
        cmd.Parameters.AddWithValue("@FirstName", user.Fornavn);
        cmd.Parameters.AddWithValue("@LastName", user.Efternavn);
        cmd.Parameters.AddWithValue("@Email", user.Email);
        cmd.Parameters.AddWithValue("@PhoneNumber", user.Telefonnummer);
        //cmd.Parameters.AddWithValue("@AddressID", adrId);
        cmd.Parameters.AddWithValue("@Avatar", sqlPath);
        cmd.Parameters.AddWithValue("@UserID", System.Web.HttpContext.Current.Session["id"]);
        cmd.ExecuteNonQuery();

型号:

public class PrivateUser
{
    public string Fødselsdag {get; set;}
    public string Fornavn { get; set; }
    public string Efternavn { get; set; }
    public string Email { get; set; }
    public int Telefonnummer { get; set; }
    public string Brugernavn { get; set; }
    public string Password { get; set; }
    public string Vejnavn { get; set; }
    public int Postnummer { get; set; }
    public string By { get; set; }
    public string Land { get; set; }
    public int Vejnummer { get; set; }
    public int Address { get; set; }
}

非常薄的模型;)

0 个答案:

没有答案