這裏是你可以嘗試什麼:
開始通過設計每次你想有與它的輸入框的頁面。然後你可以定義視圖模型,控制器和相應的視圖。它可能是這個樣子(簡單化):
的ViewModels:
/// <summary>
/// Represents a button with its text and
/// the controller name it will redirect to
/// </summary>
public class Button
{
public string Text { get; set; }
public string Controller { get; set; }
}
/// <summary>
/// Represents the page with a header and a list of buttons
/// </summary>
public class Page
{
public string Header { get; set; }
public IEnumerable<Button> Buttons { get; set; }
}
/// <summary>
/// Each view model will have page metadata
/// </summary>
public abstract class BaseViewModel
{
public Page Page { get; set; }
}
public class Page1ViewModel : BaseViewModel
{
// Put any properties specific to this page
// which will be used for the input fields
}
public class Page2ViewModel : BaseViewModel
{
// Put any properties specific to this page
// which will be used for the input fields
}
...
然後創建一個存儲庫,將解析XML(實施留給簡潔):
public interface IPagesRepository
{
Page ReadPage(string pageName);
}
那麼這裏頁面控制器的外觀如何:
public class Page1Controller : Controller
{
private readonly IPagesRepository _repository;
// TODO: to avoid repeating this ctor you could have
// a base repository controller which others derive from
public Page1Controller(IPagesRepository repository)
{
_repository = repository;
}
public ActionResult Index()
{
var model = new Page1ViewModel();
model.Page = _repository.ReadPage("page1");
//model.Page = new Page
//{
// Header = "Thanks for calling, how may I help you?",
// Buttons = new[]
// {
// new Button { Text = "Next", Controller = "Page2" },
// new Button { Text = "Address", Controller = "Page3" },
// }
//};
return View(model);
}
[HttpPost]
public ActionResult Index(Page1ViewModel model, string redirectTo)
{
if (!ModelState.IsValid)
{
return View(model);
}
return Redirect(redirectTo);
}
}
而最後一部分是更改esponding觀點:
<script type="text/javascript">
$(function() {
// when a navigation link is clicked set the redirectTo
// hidden field to the value of the controller we
// want to redirect to and submit the form
$('.nav a').click(function() {
$('form :hidden[name=redirectTo]').val(this.href);
$('form').submit();
return false;
});
});
</script>
<h2><%: Model.Page.Header %></h2>
<%: Html.ValidationSummary() %>
<% using (Html.BeginForm()) { %>
<%: Html.Hidden("redirectTo") %>
<!-- TODO: Put here the specific input fields for this page -->
<% } %>
<div class="nav">
<%: Html.EditorFor(x => x.Page.Buttons) %>
</div>
而且Button.ascx
編輯模板,這將使得導航鏈接:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeNs.Models.Button>" %>
<!-- Here we assume Index action is setup by default in the routes -->
<%: Html.ActionLink(Model.Text, "index", Model.Controller) %>
如果這種非開發者也可以定義每個屏幕(如它們的類型上的動態輸入字段 - 文本框,下拉列表,收音機,複選框,textarea,...以及他們的名字和相關標籤)? – 2010-08-31 20:57:16
我不這麼認爲。我想保留所有的模型。我知道在實際上有新字段的情況下需要更新它,但是這些腳本只是在用戶輸入的數據變化較少時才被用戶讀取。 – 2010-08-31 20:59:11
所以,如果我正確理解你的場景,你需要這個人能夠爲每個頁面定義一個標題,並指明哪一個是下一個頁面的按鈕(以前如何?),但用戶填寫的表單的設計保持固定含義他將有一個有限的頁面從他的XML中進行選擇並定義導航工作流程? – 2010-08-31 21:02:39