开发者

How to set values for Model properties on .aspx page in ASP.NET MVC 2?

I have a strongly typed view. I get model passed into the view an开发者_开发技巧d then i assign model values to labels etc.

I would then also like to set Model values programmatically on .aspx page, like:

<%= Model.someValue = "foo"; %>

and then pass that model back to controller action and than access those values. I know that I can apply values to model like these:

<%= Html.TextBoxFor(n => n.someValue) %>  

but in these case, this is not an option for me.


If the user is not supposed to modify the values of this model inside the view then you could use hidden fields or simply pass some unique identifier which will allow the controller action to retrieve back the model from the repository.


What information are you trying to set? You need to put them in form fields that will be POSTed back to the server. E.g.

public class MyModel
{
  public string Name { get; set; }

  public string UniqueCode { get; set; }
}

If can set properties if I need to:

<% Model.UniqueCode = "something"; %>
<%= Html.HiddenFor(m => m.UniqueCode) %>

And then accept those new values when the form is posted back:

public MyController : Controller
{
  public Index()
  {
    return View(new MyModel { Name = "Hello" });
  }

  [HttpPost]
  public Process(MyModel model)
  {
    string code = model.UniqueCode;
  }
}

Though why is your view modifying the model?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜