動態取得 Methods

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

動態取得 Methods

文章rusli » 週四 9月 06, 2012 2:36 pm

M
[syntax="csharp"]
public partial class HomeModel
{
public HomeModel()
{
this.Methods = new List<string>();

MethodInfo[] methods =
this.GetType().GetMethods().Where(
w =>
w.IsSpecialName == false && w.DeclaringType != null
&& w.DeclaringType.Name.Equals(this.GetType().Name)).ToArray();

foreach (MethodInfo info in methods)
{
this.Methods.Add(info.Name);
}
}

public string Method1() {}
public string Method2() {}
public string Method3() {}
public string Method4() {}
}
[/syntax]


[syntax="csharp"]
public partial class HomeModel
{
public IList<string> Methods;
}
[/syntax]

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

V

文章rusli » 週四 9月 06, 2012 2:37 pm

V

[syntax="csharp"]

@model VIdegreeApiTest.Models.HomeModel
@section featured {
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>@Model.Url</h1>
<h2></h2>
</hgroup>
<p>
</p>
</div>
</section>
}
<h3>測試</h3>
<ol class="round">
@foreach(string s in Model.Methods)
{
<li class="one">
<h5>@(s)()</h5><input type="button" id="btn@(s)" value="Send"/>
<div id="@s"></div>
</li>
}
</ol>

@section scripts {
<script type="text/javascript">
$().ready(function () {
@foreach (string s in Model.Methods)
{
@(Html.Raw("$('#btn" + s + "').click(function(){ "))
@(Html.Raw("$.post('" + Url.Action(s, "Home") + "', function(data){ $(\"#" + s + "\").html(data);});"))
@(Html.Raw("return false;});"))
}
});
</script>
}

[/syntax]

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

C

文章rusli » 週四 9月 06, 2012 2:38 pm

C

[syntax="csharp"]
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new HomeModel());
}

[HttpPost]
public string Method1()
{
return new HomeModel().Method1();
}

[HttpPost]
public string Method2()
{
return new HomeModel().Method2();
}

[HttpPost]
public string Method3()
{
return new HomeModel().Method3();
}

[HttpPost]
public string Method4()
{
return new HomeModel().Method4();
}
}

[/syntax]


回到「MVC」

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客