ASP.NET如何在客户端设置属性?

时间:2017-07-24 12:54:51

标签: asp.net

我正在尝试在将其发送到服务器之前在客户端设置一些数据,但它没有给我任何更改

div包含数据:

<div id="chanelValues" data-value="0" runat="server"></div>

复选框将提供 chanelValues div div的值

<input type="checkbox" onclick="dropdownClick(1, 'chanelValues')" />

dropdownClick func将 chanelValues 数据值设为js成功

但是服务器端给了我 chanelValues = 0

string chanelValues = this.chanelValues.Attributes["data-value"];

如何获取更新数据?

谢谢!

2 个答案:

答案 0 :(得分:1)

也许我的问题出错但我会尝试......

您需要一个模型作为服务器和客户端之间的接口

        using System;

         namespace MvcMovie.Models
         {
                public class Movie
                {
                     public int ID { get; set; }
                     public string Title { get; set; }
                     public DateTime ReleaseDate { get; set; }
                     public string Genre { get; set; }
                     public decimal Price { get; set; }
                }
         }

https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/adding-a-model

  @model MvcMovie.Models.Movie

   @{
      ViewBag.Title = "Details";
   }

  <h2>Details</h2>

  <div>
      <h4>Movie</h4>
  <hr />
      <dl class="dl-horizontal">
          <dt>
              @Html.DisplayNameFor(model => model.Title)
          </dt>
           @*Markup omitted for clarity.*@        
      </dl>
  </div>
 <p>
     @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
     @Html.ActionLink("Back to List", "Index")
 </p>

https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model

答案 1 :(得分:1)

您可以查看Update TargetID属性

控制器:

public ActionResult SomeAction()
{
    // you could return a PartialView here if you need more complex HTML fragment
    return Content("<span>some content</span>", "text/html");
}

查看:

<div id="result"></div>
<%= Ajax.ActionLink(
    "Update div test", 
    "SomeAction", 
    new AjaxOptions { UpdateTargetId = "result" }
) %>