如何在mvc中使用页面加载方法调用

本文关键字:加载 方法 调用 mvc | 更新日期: 2023-09-27 18:29:10

i wan在页面加载时调用mvc控制器方法:

    public ActionResult Detail(int id)
    {
       Customer customer =  CustomerManager.GetCustomer(id);
        return View(customer);
    }

我的视图代码:


<h2>Detail</h2>
<%=Html.ValidationSummary("Please correct the erros and try again.") %>
   <% using (Html.BeginForm("Detail", "Customer", new { id = 1 }))
      {%>
<fieldset>
<legend>Fields</legend>
<p>
<%=Html.LabelFor(q => q.id)%>
<%=Html.Encode(Model.id)%>
</p>
<p>
<%=Html.LabelFor(q => q.Name)%>
<%=Html.DisplayFor(q => q.Name)%>
</p>
<p>
<%=Html.LabelFor(q => q.SurName)%>
<%:Html.Encode(Model.SurName)%>
</p>
<p>
 <%=Html.ActionLink("Edit", "Edit", new { id = Model.id })%> 
</p>
</fieldset>
<%} %>

如何调用细节操作方法:

经典Web表单:

Pageload()
{
  if(!ispostback)
       Detail(1);
}

MVC来自。如何做到这一点?

如何在mvc中使用页面加载方法调用

if (true) // some condition
{
    return RedirectToAction("Detail", "Customer", new { id = 1 });
}